import type { DriverLivery } from '@core/racing/domain/entities/DriverLivery';
import { Result } from '@core/shared/domain/Result';
import type { GetDriverLiveriesOutputDTO } from '../dtos/GetDriverLiveriesOutputDTO';

export class GetDriverLiveriesPresenter {
  private responseModel: GetDriverLiveriesOutputDTO | null = null;

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  async present(result: Result<DriverLivery[], any>): Promise<void> {
    if (result.isErr()) {
      const error = result.unwrapErr();
      throw new Error(error.details?.message ?? 'Failed to get driver liveries');
    }

    const liveries = result.unwrap();

    const dto: GetDriverLiveriesOutputDTO = {
      liveries: liveries.map((livery, index) => ({
        id: livery.id,
        name: `Livery for ${livery.carId.toString()}`, // Simple name generation
        imageUrl: livery.uploadedImageUrl.toString(),
        createdAt: livery.createdAt.toISOString(),
        isActive: index === 0, // First livery is active by default
      })),
    };

    this.responseModel = dto;
  }

  getResponseModel(): GetDriverLiveriesOutputDTO | null {
    return this.responseModel;
  }
}