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 { RaceRegistration } from '../../domain/entities/RaceRegistration';
import type { Result as RaceResult } from '../../domain/entities/result/Result';
import type { DriverRepository } from '../../domain/repositories/DriverRepository';
import type { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
import type { RaceRegistrationRepository } from '../../domain/repositories/RaceRegistrationRepository';
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
import type { ResultRepository } from '../../domain/repositories/ResultRepository';

export type GetRaceDetailInput = {
  raceId: string;
  driverId?: string;
};

// Backwards-compatible alias for older callers
export type GetRaceDetailQueryParams = GetRaceDetailInput;

export type GetRaceDetailErrorCode = 'RACE_NOT_FOUND' | 'REPOSITORY_ERROR';

export type GetRaceDetailResult = {
  race: Race;
  league: League | null;
  registrations: RaceRegistration[];
  drivers: NonNullable<Awaited<ReturnType<DriverRepository['findById']>>>[];
  userResult: RaceResult | null;
  isUserRegistered: boolean;
  canRegister: boolean;
};

export class GetRaceDetailUseCase {
  constructor(
    private readonly raceRepository: RaceRepository,
    private readonly leagueRepository: LeagueRepository,
    private readonly driverRepository: DriverRepository,
    private readonly raceRegistrationRepository: RaceRegistrationRepository,
    private readonly resultRepository: ResultRepository,
    private readonly leagueMembershipRepository: LeagueMembershipRepository,
  ) {}

  async execute(
    input: GetRaceDetailInput,
  ): Promise<Result<GetRaceDetailResult, ApplicationErrorCode<GetRaceDetailErrorCode, { message: string }>>> {
    const { raceId, driverId } = input;

    try {
      const race = await this.raceRepository.findById(raceId);
      if (!race) {
        return Result.err({
          code: 'RACE_NOT_FOUND',
          details: { message: 'Race not found' },
        });
      }

      const [league, registrations, membership] = await Promise.all([
        this.leagueRepository.findById(race.leagueId),
        this.raceRegistrationRepository.findByRaceId(race.id),
        driverId ? this.leagueMembershipRepository.getMembership(race.leagueId, driverId) : Promise.resolve(null),
      ]);

      const drivers = await Promise.all(
        registrations.map(registration => this.driverRepository.findById(registration.driverId.toString())),
      );

      const validDrivers = drivers.filter((driver): driver is NonNullable<typeof driver> => driver !== null);

      const isUserRegistered =
        typeof driverId === 'string' && driverId.length > 0
          ? registrations.some(reg => reg.driverId.toString() === driverId)
          : false;

      const isUpcoming = race.status.isScheduled() && race.scheduledAt > new Date();
      const canRegister =
        typeof driverId === 'string' && driverId.length > 0
          ? !!membership && membership.status.toString() === 'active' && isUpcoming
          : false;

      let userResult: RaceResult | null = null;

      if (race.status.isCompleted() && typeof driverId === 'string' && driverId.length > 0) {
        const results = await this.resultRepository.findByRaceId(race.id);
        userResult = results.find(r => r.driverId.toString() === driverId) ?? null;
      }

      const result: GetRaceDetailResult = {
        race,
        league: league ?? null,
        registrations,
        drivers: validDrivers,
        userResult,
        isUserRegistered,
        canRegister,
      };

      return Result.ok(result);
    } catch (error: unknown) {
      const message =
        error instanceof Error && error.message
          ? error.message
          : 'Failed to load race detail';

      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: { message },
      });
    }
  }
}
