/**
 * Providers Component
 *
 * Client-side providers wrapper for the application
 */

'use client';

import { useState } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { AuthProvider } from '@/lib/context/auth-context';
import { CookieProvider } from '@/lib/context/cookie-context';
import CookieConsent from '@/components/CookieConsent';

export function Providers({ children }: { children: React.ReactNode }) {
  // Create a client instance per component instance
  // This ensures the QueryClient is stable across rerenders
  const [queryClient] = useState(
    () =>
      new QueryClient({
        defaultOptions: {
          queries: {
            staleTime: 60 * 1000, // 1 minute
            refetchOnWindowFocus: false,
          },
        },
      })
  );

  return (
    <QueryClientProvider client={queryClient}>
      <AuthProvider>
        <CookieProvider>
          {children}
          <CookieConsent />
        </CookieProvider>
      </AuthProvider>
    </QueryClientProvider>
  );
}
