import type { UseCaseOutputPort } from '@core/shared/application';
import type { GetLeagueWalletResult } from '@core/racing/application/use-cases/GetLeagueWalletUseCase';
import { GetLeagueWalletOutputDTO, WalletTransactionDTO } from '../dtos/GetLeagueWalletOutputDTO';

export class GetLeagueWalletPresenter implements UseCaseOutputPort<GetLeagueWalletResult> {
  private model: GetLeagueWalletOutputDTO | null = null;

  present(result: GetLeagueWalletResult): void {
    const transactions: WalletTransactionDTO[] = result.transactions.map(tx => ({
      id: tx.id.toString(),
      type: tx.type as 'sponsorship' | 'membership' | 'withdrawal' | 'prize',
      description: tx.description ?? '',
      amount: tx.amount.amount,
      fee: tx.platformFee.amount,
      netAmount: tx.netAmount.amount,
      date: tx.createdAt.toISOString(),
      status: tx.status === 'cancelled' ? 'failed' : tx.status,
    }));

    this.model = {
      balance: result.aggregates.balance.amount,
      currency: result.aggregates.balance.currency,
      totalRevenue: result.aggregates.totalRevenue.amount,
      totalFees: result.aggregates.totalFees.amount,
      totalWithdrawals: result.aggregates.totalWithdrawals.amount,
      pendingPayouts: result.aggregates.pendingPayouts.amount,
      canWithdraw: result.wallet.canWithdraw(result.aggregates.balance),
      transactions,
    };
  }

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

  get responseModel(): GetLeagueWalletOutputDTO {
    if (!this.model) throw new Error('Presenter not presented');
    return this.model;
  }
}