import type { GetLeagueSeasonsResult } from '@core/racing/application/use-cases/GetLeagueSeasonsUseCase';
import type { Presenter } from '../../../shared/presentation/Presenter';
import { LeagueSeasonSummaryDTO } from '../dtos/LeagueSeasonSummaryDTO';

export class GetLeagueSeasonsPresenter implements Presenter<GetLeagueSeasonsResult, LeagueSeasonSummaryDTO[]> {
  private result: LeagueSeasonSummaryDTO[] | null = null;

  reset() {
    this.result = null;
  }

  present(input: GetLeagueSeasonsResult) {
    this.result = input.seasons.map((seasonSummary) => {
      const dto = new LeagueSeasonSummaryDTO();
      dto.seasonId = seasonSummary.season.id.toString();
      dto.name = seasonSummary.season.name.toString();
      dto.status = seasonSummary.season.status.toString();

      if (seasonSummary.season.startDate) dto.startDate = seasonSummary.season.startDate;
      if (seasonSummary.season.endDate) dto.endDate = seasonSummary.season.endDate;

      dto.isPrimary = seasonSummary.isPrimary;
      dto.isParallelActive = seasonSummary.isParallelActive;

      // Calculate mock data for new fields
      const now = new Date();
      const totalRaces = seasonSummary.season.schedule?.plannedRounds || 0;
      
      // Calculate completed races based on schedule
      let completedRaces = 0;
      if (seasonSummary.season.schedule && seasonSummary.season.startDate) {
        const startDate = seasonSummary.season.startDate;
        const recurrence = seasonSummary.season.schedule.recurrence;
        
        // Calculate how many races would have been completed by now
        const daysSinceStart = Math.floor((now.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
        const weeksSinceStart = Math.floor(daysSinceStart / 7);
        
        // For weekly recurrence, calculate completed races
        if (recurrence.props.kind === 'weekly') {
          completedRaces = Math.min(weeksSinceStart, totalRaces);
        } else {
          // For other recurrence types, use a simple calculation
          completedRaces = Math.min(Math.floor(weeksSinceStart * 0.5), totalRaces);
        }
      }

      // Calculate next race date
      let nextRaceAt: Date | undefined;
      if (seasonSummary.season.schedule && seasonSummary.season.startDate) {
        const startDate = seasonSummary.season.startDate;
        const recurrence = seasonSummary.season.schedule.recurrence;
        
        if (recurrence.props.kind === 'weekly') {
          const daysSinceStart = Math.floor((now.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
          const weeksSinceStart = Math.floor(daysSinceStart / 7);
          const nextRaceWeek = weeksSinceStart + 1;
          
          if (nextRaceWeek <= totalRaces) {
            const nextRaceDate = new Date(startDate);
            nextRaceDate.setDate(startDate.getDate() + (nextRaceWeek * 7));
            nextRaceAt = nextRaceDate;
          }
        }
      }

      dto.totalRaces = totalRaces;
      dto.completedRaces = completedRaces;
      if (nextRaceAt) {
        dto.nextRaceAt = nextRaceAt;
      }

      return dto;
    });
  }

  getResponseModel(): LeagueSeasonSummaryDTO[] | null {
    return this.result;
  }
}