/**
 * Application Use Case: DeletePrizeUseCase
 *
 * Deletes a prize.
 */

import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
import type {
  IDeletePrizePresenter,
  DeletePrizeResultDTO,
  DeletePrizeViewModel,
} from '../presenters/IDeletePrizePresenter';
import type { UseCase } from '@core/shared/application/UseCase';

export interface DeletePrizeInput {
  prizeId: string;
}

export class DeletePrizeUseCase
  implements UseCase<DeletePrizeInput, DeletePrizeResultDTO, DeletePrizeViewModel, IDeletePrizePresenter>
{
  constructor(private readonly prizeRepository: IPrizeRepository) {}

  async execute(
    input: DeletePrizeInput,
    presenter: IDeletePrizePresenter,
  ): Promise<void> {
    presenter.reset();

    const { prizeId } = input;

    const prize = await this.prizeRepository.findById(prizeId);
    if (!prize) {
      throw new Error('Prize not found');
    }

    if (prize.awarded) {
      throw new Error('Cannot delete an awarded prize');
    }

    await this.prizeRepository.delete(prizeId);

    const dto: DeletePrizeResultDTO = {
      success: true,
    };

    presenter.present(dto);
  }
}