import type { GetRacePenaltiesResult } from '@core/racing/application/use-cases/GetRacePenaltiesUseCase';
import type { RacePenaltiesDTO } from '../dtos/RacePenaltiesDTO';
import type { RacePenaltyDTO } from '../dtos/RacePenaltyDTO';

export type GetRacePenaltiesResponseModel = RacePenaltiesDTO;

export class RacePenaltiesPresenter {
  private model: GetRacePenaltiesResponseModel | null = null;

  reset(): void {
    this.model = null;
  }

  present(result: GetRacePenaltiesResult): void {
    const penalties: RacePenaltyDTO[] = result.penalties.map(penalty => ({
      id: penalty.id.toString(),
      driverId: penalty.driverId,
      type: penalty.type,
      value: penalty.value ?? 0,
      reason: penalty.reason,
      issuedBy: penalty.issuedBy,
      issuedAt: penalty.issuedAt.toISOString(),
      notes: penalty.notes,
    } as RacePenaltyDTO));

    const driverMap: Record<string, string> = {};
    result.drivers.forEach(driver => {
      driverMap[driver.id] = driver.name.toString();
    });

    this.model = {
      penalties,
      driverMap,
    } as RacePenaltiesDTO;
  }

  getResponseModel(): GetRacePenaltiesResponseModel | null {
    return this.model;
  }

  get responseModel(): GetRacePenaltiesResponseModel {
    if (!this.model) {
      throw new Error('Presenter not presented');
    }

    return this.model;
  }

  get viewModel(): GetRacePenaltiesResponseModel {
    return this.responseModel;
  }
}