/**
 * Application Use Case: GetPaymentsUseCase
 *
 * Retrieves payments based on filters.
 */

import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
import type { PaymentType } from '../../domain/entities/Payment';
import type {
  IGetPaymentsPresenter,
  GetPaymentsResultDTO,
  GetPaymentsViewModel,
} from '../presenters/IGetPaymentsPresenter';
import type { UseCase } from '@core/shared/application/UseCase';

export interface GetPaymentsInput {
  leagueId?: string;
  payerId?: string;
  type?: PaymentType;
}

export class GetPaymentsUseCase
  implements UseCase<GetPaymentsInput, GetPaymentsResultDTO, GetPaymentsViewModel, IGetPaymentsPresenter>
{
  constructor(private readonly paymentRepository: IPaymentRepository) {}

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

    const payments = await this.paymentRepository.findByFilters({
      leagueId: input.leagueId,
      payerId: input.payerId,
      type: input.type,
    });

    const dto: GetPaymentsResultDTO = {
      payments: payments.map(payment => ({
        id: payment.id,
        type: payment.type,
        amount: payment.amount,
        platformFee: payment.platformFee,
        netAmount: payment.netAmount,
        payerId: payment.payerId,
        payerType: payment.payerType,
        leagueId: payment.leagueId,
        seasonId: payment.seasonId,
        status: payment.status,
        createdAt: payment.createdAt,
        completedAt: payment.completedAt,
      })),
    };

    presenter.present(dto);
  }
}