import { Result } from '@core/shared/application/Result';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
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 { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { IResultRepository } from '../../domain/repositories/IResultRepository';

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<IDriverRepository['findById']>>>[];
  userResult: RaceResult | null;
  isUserRegistered: boolean;
  canRegister: boolean;
};

export class GetRaceDetailUseCase {
  constructor(
    private readonly raceRepository: IRaceRepository,
    private readonly leagueRepository: ILeagueRepository,
    private readonly driverRepository: IDriverRepository,
    private readonly raceRegistrationRepository: IRaceRegistrationRepository,
    private readonly resultRepository: IResultRepository,
    private readonly leagueMembershipRepository: ILeagueMembershipRepository,
    private readonly output: UseCaseOutputPort<GetRaceDetailResult>,
  ) {}

  async execute(
    input: GetRaceDetailInput,
  ): Promise<Result<void, 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,
      };

      this.output.present(result);

      return Result.ok(undefined);
    } 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 },
      });
    }
  }
}