import type { GetPaymentsResult } from '@core/payments/application/use-cases/GetPaymentsUseCase';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { GetPaymentsOutput } from '../dtos/PaymentsDto';

export class GetPaymentsPresenter implements UseCaseOutputPort<GetPaymentsResult> {
  private responseModel: GetPaymentsOutput | null = null;

  reset() {
    this.responseModel = null;
  }

  present(result: GetPaymentsResult): void {
    this.responseModel = {
      payments: result.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,
        ...(payment.seasonId !== undefined ? { seasonId: payment.seasonId } : {}),
        status: payment.status,
        createdAt: payment.createdAt,
        ...(payment.completedAt !== undefined ? { completedAt: payment.completedAt } : {}),
      })),
    };
  }

  getResponseModel(): GetPaymentsOutput {
    if (!this.responseModel) throw new Error('Presenter not presented');
    return this.responseModel;
  }
}