import type { GetSponsorSponsorshipsResult } from '@core/racing/application/use-cases/GetSponsorSponsorshipsUseCase';
import { SponsorSponsorshipsDTO } from '../dtos/SponsorSponsorshipsDTO';

export class GetSponsorSponsorshipsPresenter {
  private result: SponsorSponsorshipsDTO | null = null;

  reset() {
    this.result = null;
  }

  present(outputPort: GetSponsorSponsorshipsResult | null) {
    if (!outputPort) {
      this.result = null;
      return;
    }

    this.result = {
      sponsorId: outputPort.sponsor.id.toString(),
      sponsorName: outputPort.sponsor.name.toString(),
      sponsorships: outputPort.sponsorships.map(s => {
        // Map status to DTO expected values
        let status: 'pending' | 'active' | 'expired' | 'cancelled';
        if (s.sponsorship.status === 'ended') {
          status = 'expired';
        } else if (s.sponsorship.status === 'pending' || s.sponsorship.status === 'active' || s.sponsorship.status === 'cancelled') {
          status = s.sponsorship.status;
        } else {
          status = 'pending';
        }
        
        return {
          id: s.sponsorship.id.toString(),
          leagueId: s.league.id.toString(),
          leagueName: s.league.name.toString(),
          seasonId: s.season.id.toString(),
          seasonName: s.season.name.toString(),
          tier: s.sponsorship.tier,
          status,
          pricing: {
            amount: s.financials.pricing.amount,
            currency: s.financials.pricing.currency,
          },
          platformFee: {
            amount: s.financials.platformFee.amount,
            currency: s.financials.platformFee.currency,
          },
          netAmount: {
            amount: s.financials.netAmount.amount,
            currency: s.financials.netAmount.currency,
          },
          metrics: s.metrics,
          createdAt: s.sponsorship.createdAt,
        };
      }),
      summary: {
        totalSponsorships: outputPort.summary.totalSponsorships,
        activeSponsorships: outputPort.summary.activeSponsorships,
        totalInvestment: outputPort.summary.totalInvestment.amount,
        totalPlatformFees: outputPort.summary.totalPlatformFees.amount,
        currency: outputPort.summary.totalInvestment.currency,
      },
    };
  }

  getViewModel(): SponsorSponsorshipsDTO | null {
    return this.result;
  }

  get viewModel(): SponsorSponsorshipsDTO {
    if (!this.result) throw new Error('Presenter not presented');
    return this.result;
  }
}
