import type { GetPendingSponsorshipRequestsResult } from '@core/racing/application/use-cases/GetPendingSponsorshipRequestsUseCase';
import { GetPendingSponsorshipRequestsOutputDTO } from '../dtos/GetPendingSponsorshipRequestsOutputDTO';
import { SponsorshipRequestDTO } from '../dtos/SponsorshipRequestDTO';

export class GetPendingSponsorshipRequestsPresenter {
  private result: GetPendingSponsorshipRequestsOutputDTO | null = null;

  reset() {
    this.result = null;
  }

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

    this.result = {
      entityType: outputPort.entityType,
      entityId: outputPort.entityId,
      requests: outputPort.requests.map(r => {
        const request = new SponsorshipRequestDTO();
        request.id = r.request.id;
        request.sponsorId = r.request.sponsorId;
        request.sponsorName = r.sponsor?.name?.toString() || 'Unknown Sponsor';
        request.tier = r.request.tier;
        request.offeredAmount = r.financials.offeredAmount.amount;
        request.currency = r.financials.offeredAmount.currency;
        request.formattedAmount = `${r.financials.offeredAmount.amount} ${r.financials.offeredAmount.currency}`;
        request.createdAt = r.request.createdAt;
        request.platformFee = r.financials.platformFee.amount;
        request.netAmount = r.financials.netAmount.amount;

        if (r.sponsor?.logoUrl) {
          request.sponsorLogo = r.sponsor.logoUrl.toString();
        }

        if (r.request.message) {
          request.message = r.request.message;
        }

        return request;
      }),
      totalCount: outputPort.totalCount,
    };
  }

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

  get viewModel(): GetPendingSponsorshipRequestsOutputDTO | null {
    return this.result;
  }
}
