'use client';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { createContext, ReactNode, useContext, useMemo } from 'react';
import { ServiceFactory } from './ServiceFactory';

// Import all service types
import { AnalyticsService } from './analytics/AnalyticsService';
import { AuthService } from './auth/AuthService';
import { SessionService } from './auth/SessionService';
import { DashboardService } from './dashboard/DashboardService';
import { DriverRegistrationService } from './drivers/DriverRegistrationService';
import { DriverService } from './drivers/DriverService';
import { LeagueMembershipService } from './leagues/LeagueMembershipService';
import { LeagueService } from './leagues/LeagueService';
import { LeagueSettingsService } from './leagues/LeagueSettingsService';
import { LeagueStewardingService } from './leagues/LeagueStewardingService';
import { LeagueWalletService } from './leagues/LeagueWalletService';
import { AvatarService } from './media/AvatarService';
import { MediaService } from './media/MediaService';
import { MembershipFeeService } from './payments/MembershipFeeService';
import { PaymentService } from './payments/PaymentService';
import { WalletService } from './payments/WalletService';
import { PenaltyService } from './penalties/PenaltyService';
import { ProtestService } from './protests/ProtestService';
import { RaceResultsService } from './races/RaceResultsService';
import { RaceService } from './races/RaceService';
import { RaceStewardingService } from './races/RaceStewardingService';
import { SponsorService } from './sponsors/SponsorService';
import { SponsorshipService } from './sponsors/SponsorshipService';
import { TeamJoinService } from './teams/TeamJoinService';
import { TeamService } from './teams/TeamService';

export interface Services {
  raceService: RaceService;
  raceResultsService: RaceResultsService;
  raceStewardingService: RaceStewardingService;
  driverService: DriverService;
  driverRegistrationService: DriverRegistrationService;
  teamService: TeamService;
  teamJoinService: TeamJoinService;
  leagueService: LeagueService;
  leagueMembershipService: LeagueMembershipService;
  leagueSettingsService: LeagueSettingsService;
  leagueStewardingService: LeagueStewardingService;
  leagueWalletService: LeagueWalletService;
  sponsorService: SponsorService;
  sponsorshipService: SponsorshipService;
  paymentService: PaymentService;
  analyticsService: AnalyticsService;
  dashboardService: DashboardService;
  mediaService: MediaService;
  avatarService: AvatarService;
  walletService: WalletService;
  membershipFeeService: MembershipFeeService;
  authService: AuthService;
  sessionService: SessionService;
  protestService: ProtestService;
  penaltyService: PenaltyService;
}

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 5 * 60 * 1000, // 5 minutes
      gcTime: 10 * 60 * 1000, // 10 minutes
    },
  },
});

const ServicesContext = createContext<Services | null>(null);

interface ServiceProviderProps {
  children: ReactNode;
}

export function ServiceProvider({ children }: ServiceProviderProps) {
  const services = useMemo(() => {
    const serviceFactory = new ServiceFactory(process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001');

    return {
      raceService: serviceFactory.createRaceService(),
      raceResultsService: serviceFactory.createRaceResultsService(),
      raceStewardingService: serviceFactory.createRaceStewardingService(),
      driverService: serviceFactory.createDriverService(),
      driverRegistrationService: serviceFactory.createDriverRegistrationService(),
      teamService: serviceFactory.createTeamService(),
      teamJoinService: serviceFactory.createTeamJoinService(),
      leagueService: serviceFactory.createLeagueService(),
      leagueMembershipService: serviceFactory.createLeagueMembershipService(),
      leagueSettingsService: serviceFactory.createLeagueSettingsService(),
      leagueStewardingService: serviceFactory.createLeagueStewardingService(),
      leagueWalletService: serviceFactory.createLeagueWalletService(),
      sponsorService: serviceFactory.createSponsorService(),
      sponsorshipService: serviceFactory.createSponsorshipService(),
      paymentService: serviceFactory.createPaymentService(),
      analyticsService: serviceFactory.createAnalyticsService(),
      dashboardService: serviceFactory.createDashboardService(),
      mediaService: serviceFactory.createMediaService(),
      avatarService: serviceFactory.createAvatarService(),
      walletService: serviceFactory.createWalletService(),
      membershipFeeService: serviceFactory.createMembershipFeeService(),
      authService: serviceFactory.createAuthService(),
      sessionService: serviceFactory.createSessionService(),
      protestService: serviceFactory.createProtestService(),
      penaltyService: serviceFactory.createPenaltyService(),
    };
  }, []);

  return (
    <QueryClientProvider client={queryClient}>
      <ServicesContext.Provider value={services}>
        {children}
      </ServicesContext.Provider>
    </QueryClientProvider>
  );
}
// Before using this check for enhanced hooks that use react-query
export function useServices(): Services {
  const services = useContext(ServicesContext);
  if (!services) {
    throw new Error('useServices must be used within a ServiceProvider');
  }
  return services;
}