import type { GetRaceProtestsResult } from '@core/racing/application/use-cases/GetRaceProtestsUseCase';
import type { RaceProtestDTO } from '../dtos/RaceProtestDTO';
import type { RaceProtestsDTO } from '../dtos/RaceProtestsDTO';

export type GetRaceProtestsResponseModel = RaceProtestsDTO;

export class RaceProtestsPresenter {
  private model: GetRaceProtestsResponseModel | null = null;

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

  present(result: GetRaceProtestsResult): void {
    const protests: RaceProtestDTO[] = result.protests.map(protest => ({
      id: protest.id,
      protestingDriverId: protest.protestingDriverId,
      accusedDriverId: protest.accusedDriverId,
      incident: {
        lap: protest.incident.lap.toNumber(),
        description: protest.incident.description.toString(),
      },
      status: protest.status.toString(),
      filedAt: protest.filedAt.toISOString(),
    } as RaceProtestDTO));

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

    this.model = {
      protests,
      driverMap,
    } as RaceProtestsDTO;
  }

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

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

    return this.model;
  }

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