import { PageWrapper } from '@/components/shared/state/PageWrapper';
import { LeagueStandingsTemplate } from '@/templates/LeagueStandingsTemplate';
import { PageDataFetcher } from '@/lib/page/PageDataFetcher';
import { LeagueService } from '@/lib/services/leagues/LeagueService';
import { LeaguesApiClient } from '@/lib/api/leagues/LeaguesApiClient';
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { notFound } from 'next/navigation';
import { StandingEntryViewModel } from '@/lib/view-models/StandingEntryViewModel';
import { DriverViewModel } from '@/lib/view-models/DriverViewModel';
import type { LeagueMembership } from '@/lib/types/LeagueMembership';
import type { LeagueStandingDTO } from '@/lib/types/generated/LeagueStandingDTO';
import type { LeagueMemberDTO } from '@/lib/types/generated/LeagueMemberDTO';
import { LeagueStandingsPresenter } from '@/lib/presenters/LeagueStandingsPresenter';

interface Props {
  params: { id: string };
}

export default async function Page({ params }: Props) {
  // Validate params
  if (!params.id) {
    notFound();
  }

  // Fetch data using PageDataFetcher.fetchManual for multiple dependencies
  const data = await PageDataFetcher.fetchManual(async () => {
    // Create dependencies
    const baseUrl = getWebsiteApiBaseUrl();
    const logger = new ConsoleLogger();
    const errorReporter = new EnhancedErrorReporter(logger, {
      showUserNotifications: true,
      logToConsole: true,
      reportToExternal: process.env.NODE_ENV === 'production',
    });

    // Create API clients
    const leaguesApiClient = new LeaguesApiClient(baseUrl, errorReporter, logger);
    const driversApiClient = new DriversApiClient(baseUrl, errorReporter, logger);
    const sponsorsApiClient = new SponsorsApiClient(baseUrl, errorReporter, logger);
    const racesApiClient = new RacesApiClient(baseUrl, errorReporter, logger);

    // Create service
    const service = new LeagueService(
      leaguesApiClient,
      driversApiClient,
      sponsorsApiClient,
      racesApiClient
    );

    // Fetch data
    const standingsDto = await service.getLeagueStandings(params.id);
    if (!standingsDto) {
      throw new Error('League standings not found');
    }
    
    // Get memberships for transformation
    const membershipsDto = await service.getLeagueMemberships(params.id);
    
    // Transform standings to StandingEntryViewModel[]
    const standings: LeagueStandingDTO[] = standingsDto.standings || [];
    const leaderPoints = standings[0]?.points || 0;
    const standingViewModels = standings.map((entry, index) => {
      const nextPoints = standings[index + 1]?.points || entry.points;
      return new StandingEntryViewModel(entry, leaderPoints, nextPoints, '', undefined);
    });
    
    // Extract unique drivers from standings and convert to DriverViewModel[]
    const driverMap = new Map<string, DriverViewModel>();
    standings.forEach(standing => {
      if (standing.driver && !driverMap.has(standing.driver.id)) {
        const driver = standing.driver;
        driverMap.set(driver.id, new DriverViewModel({
          id: driver.id,
          name: driver.name,
          avatarUrl: null, // DriverDTO doesn't have avatarUrl
          iracingId: driver.iracingId,
          rating: undefined, // DriverDTO doesn't have rating
          country: driver.country,
        }));
      }
    });
    const drivers = Array.from(driverMap.values());
    
    // Transform memberships
    const memberships: LeagueMembership[] = (membershipsDto.members || []).map((m: LeagueMemberDTO) => ({
      driverId: m.driverId,
      leagueId: params.id,
      role: (m.role as LeagueMembership['role']) ?? 'member',
      joinedAt: m.joinedAt,
      status: 'active' as const,
    }));

    return {
      standings: standingViewModels,
      drivers,
      memberships,
    };
  });

  if (!data) {
    notFound();
  }

  // Create a wrapper component that passes ViewData to the template
  const TemplateWrapper = () => {
    // Convert ViewModels to ViewData using Presenter
    const viewData = LeagueStandingsPresenter.createViewData(
      data.standings,
      data.drivers,
      data.memberships,
      params.id,
      null, // currentDriverId
      false // isAdmin
    );
    
    return (
      <LeagueStandingsTemplate
        viewData={viewData}
        onRemoveMember={() => {}}
        onUpdateRole={() => {}}
      />
    );
  };

  return (
    <PageWrapper
      data={data}
      Template={TemplateWrapper}
      loading={{ variant: 'skeleton', message: 'Loading standings...' }}
      errorConfig={{ variant: 'full-screen' }}
      empty={{
        title: 'Standings not found',
        description: 'The standings for this league are not available.',
      }}
    />
  );
}