import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { Logger } from '@core/shared/application';
import type {
  AllRacesPageResultDTO,
  AllRacesPageViewModel,
  AllRacesListItemViewModel,
  AllRacesFilterOptionsViewModel,
} from '../presenters/IAllRacesPagePresenter';
import type { AsyncUseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

export class GetAllRacesPageDataUseCase
  implements AsyncUseCase<void, AllRacesPageResultDTO, 'REPOSITORY_ERROR'> {
  constructor(
    private readonly raceRepository: IRaceRepository,
    private readonly leagueRepository: ILeagueRepository,
    private readonly logger: Logger,
  ) {}

  async execute(): Promise<Result<AllRacesPageResultDTO, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
    this.logger.debug('Executing GetAllRacesPageDataUseCase');
    try {
      const [allRaces, allLeagues] = await Promise.all([
        this.raceRepository.findAll(),
        this.leagueRepository.findAll(),
      ]);
      this.logger.info(`Found ${allRaces.length} races and ${allLeagues.length} leagues.`);

      const leagueMap = new Map(allLeagues.map((league) => [league.id.toString(), league.name.toString()]));

      const races: AllRacesListItemViewModel[] = allRaces
        .slice()
        .sort((a, b) => b.scheduledAt.getTime() - a.scheduledAt.getTime())
        .map((race) => ({
          id: race.id,
          track: race.track,
          car: race.car,
          scheduledAt: race.scheduledAt.toISOString(),
          status: race.status,
          leagueId: race.leagueId,
          leagueName: leagueMap.get(race.leagueId) ?? 'Unknown League',
          strengthOfField: race.strengthOfField ?? null,
        }));

      const uniqueLeagues = new Map<string, { id: string; name: string }>();
      for (const league of allLeagues) {
        uniqueLeagues.set(league.id.toString(), { id: league.id.toString(), name: league.name.toString() });
      }

      const filters: AllRacesFilterOptionsViewModel = {
        statuses: [
          { value: 'all', label: 'All Statuses' },
          { value: 'scheduled', label: 'Scheduled' },
          { value: 'running', label: 'Live' },
          { value: 'completed', label: 'Completed' },
          { value: 'cancelled', label: 'Cancelled' },
        ],
        leagues: Array.from(uniqueLeagues.values()),
      };

      const viewModel: AllRacesPageViewModel = {
        races,
        filters,
      };

      this.logger.debug('Successfully retrieved all races page data.');
      return Result.ok(viewModel);
    } catch (error) {
      this.logger.error('Error executing GetAllRacesPageDataUseCase', error instanceof Error ? error : new Error(String(error)));
      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: { message: error instanceof Error ? error.message : 'Unknown error occurred' },
      });
    }
  }
}