/**
 * 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 { SubscriptionProvider } from '@/lib/context/subscription-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>
        <SubscriptionProvider>
          <CookieProvider>
            {children}
            <CookieConsent />
          </CookieProvider>
        </SubscriptionProvider>
      </AuthProvider>
    </QueryClientProvider>
  );
}
