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

import type { UseCase } from '@core/shared/application/UseCase';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { PrizeRepository } from '../../domain/repositories/PrizeRepository';

export interface DeletePrizeInput {
  prizeId: string;
}

export interface DeletePrizeResult {
  success: boolean;
}

export type DeletePrizeErrorCode = 'PRIZE_NOT_FOUND' | 'CANNOT_DELETE_AWARDED_PRIZE';

export class DeletePrizeUseCase
  implements UseCase<DeletePrizeInput, DeletePrizeResult, DeletePrizeErrorCode>
{
  constructor(
    private readonly prizeRepository: PrizeRepository,
  ) {}

  async execute(input: DeletePrizeInput): Promise<Result<DeletePrizeResult, ApplicationErrorCode<DeletePrizeErrorCode>>> {
    const { prizeId } = input;

    const prize = await this.prizeRepository.findById(prizeId);
    if (!prize) {
      return Result.err({ code: 'PRIZE_NOT_FOUND' as const });
    }

    if (prize.awarded) {
      return Result.err({ code: 'CANNOT_DELETE_AWARDED_PRIZE' as const });
    }

    await this.prizeRepository.delete(prizeId);

    return Result.ok({ success: true });
  }
}