import type { Logger } from '@core/shared/domain/Logger';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { League } from '../../domain/entities/League';
import type { Race } from '../../domain/entities/Race';
import type { Season } from '../../domain/entities/season/Season';
import { LeagueRepository } from '../../domain/repositories/LeagueRepository';
import { RaceRepository } from '../../domain/repositories/RaceRepository';
import { SeasonRepository } from '../../domain/repositories/SeasonRepository';
import { SeasonScheduleGenerator } from '../../domain/services/SeasonScheduleGenerator';

export type GetLeagueScheduleErrorCode =
  | 'LEAGUE_NOT_FOUND'
  | 'SEASON_NOT_FOUND'
  | 'REPOSITORY_ERROR';

export interface GetLeagueScheduleInput {
  leagueId: string;
  seasonId?: string;
}

export interface LeagueScheduledRace {
  race: Race;
}

export interface GetLeagueScheduleResult {
  league: League;
  seasonId: string;
  published: boolean;
  races: LeagueScheduledRace[];
}

export class GetLeagueScheduleUseCase {
  constructor(
    private readonly leagueRepository: LeagueRepository,
    private readonly seasonRepository: SeasonRepository,
    private readonly raceRepository: RaceRepository,
    private readonly logger: Logger,
  ) {}

  private async resolveSeasonForSchedule(params: {
    leagueId: string;
    requestedSeasonId?: string;
  }): Promise<Result<Season | null, ApplicationErrorCode<GetLeagueScheduleErrorCode, { message: string }>>> {
    if (params.requestedSeasonId) {
      const season = await this.seasonRepository.findById(params.requestedSeasonId);
      if (!season || season.leagueId !== params.leagueId) {
        return Result.err({
          code: 'SEASON_NOT_FOUND',
          details: { message: 'Season not found for league' },
        });
      }
      return Result.ok(season);
    }

    const seasons = await this.seasonRepository.findByLeagueId(params.leagueId);
    const activeSeason = seasons.find((s: Season) => s.status.isActive()) ?? seasons[0];
    if (!activeSeason) {
      // Return null instead of error - this allows showing all races for the league
      return Result.ok(null);
    }

    return Result.ok(activeSeason);
  }

  private getSeasonDateWindow(season: Season): { start?: Date; endInclusive?: Date } {
    const start = season.startDate ?? season.schedule?.startDate;
    const window: { start?: Date; endInclusive?: Date } = {};

    if (start) {
      window.start = start;
    }

    if (season.endDate) {
      window.endInclusive = season.endDate;
      return window;
    }

    if (season.schedule) {
      const slots = SeasonScheduleGenerator.generateSlotsUpTo(
        season.schedule,
        season.schedule.plannedRounds,
      );
      const last = slots.at(-1);
      if (last?.scheduledAt) {
        window.endInclusive = last.scheduledAt;
      }
      return window;
    }

    return window;
  }

  private filterRacesBySeasonWindow(season: Season, races: Race[]): Race[] {
    const { start, endInclusive } = this.getSeasonDateWindow(season);

    if (!start && !endInclusive) return races;

    return races.filter(race => {
      const t = race.scheduledAt.getTime();
      if (start && t < start.getTime()) return false;
      if (endInclusive && t > endInclusive.getTime()) return false;
      return true;
    });
  }

  async execute(
    input: GetLeagueScheduleInput,
  ): Promise<
    Result<GetLeagueScheduleResult, ApplicationErrorCode<GetLeagueScheduleErrorCode, { message: string }>>
  > {
    this.logger.debug('Fetching league schedule', { input });
    const { leagueId } = input;

    try {
      const league = await this.leagueRepository.findById(leagueId);
      if (!league) {
        this.logger.warn('League not found when fetching schedule', { leagueId });
        return Result.err({
          code: 'LEAGUE_NOT_FOUND',
          details: { message: 'League not found' },
        });
      }

      const seasonResult = await this.resolveSeasonForSchedule({
        leagueId,
        ...(input.seasonId ? { requestedSeasonId: input.seasonId } : {}),
      });
      if (seasonResult.isErr()) {
        return Result.err(seasonResult.unwrapErr());
      }
      const season = seasonResult.unwrap();

      const races = await this.raceRepository.findByLeagueId(leagueId);
      
      // If no season exists, show all races for the league
      const seasonRaces = season ? this.filterRacesBySeasonWindow(season, races) : races;

      const scheduledRaces: LeagueScheduledRace[] = seasonRaces.map(race => ({
        race,
      }));

      const result: GetLeagueScheduleResult = {
        league,
        seasonId: season?.id ?? 'no-season',
        published: season?.schedulePublished ?? false,
        races: scheduledRaces,
      };

      return Result.ok(result);
    } catch (error) {
      this.logger.error(
        'Failed to load league schedule due to an unexpected error',
        error instanceof Error ? error : new Error('Unknown error'),
      );

      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: {
          message:
            error instanceof Error
              ? error.message
              : 'Failed to load league schedule',
        },
      });
    }
  }
}