import type { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type {
  GetTotalRacesResult,
  GetTotalRacesErrorCode,
} from '@core/racing/application/use-cases/GetTotalRacesUseCase';
import type { RaceStatsDTO } from '../dtos/RaceStatsDTO';

export type GetTotalRacesResponseModel = RaceStatsDTO;

export type GetTotalRacesApplicationError = ApplicationErrorCode<
  GetTotalRacesErrorCode,
  { message: string }
>;

export class GetTotalRacesPresenter {
  private model: GetTotalRacesResponseModel | null = null;

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

  present(result: Result<GetTotalRacesResult, GetTotalRacesApplicationError>): void {
    if (result.isErr()) {
      const error = result.unwrapErr();
      throw new Error(error.details?.message ?? 'Failed to get total races');
    }

    const output = result.unwrap();

    this.model = {
      totalRaces: output.totalRaces,
    };
  }

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

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

    return this.model;
  }

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