/**
 * Application Use Case: GetPendingSponsorshipRequestsUseCase
 *
 * Retrieves pending sponsorship requests for an entity owner to review.
 */

import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Sponsor } from '../../domain/entities/sponsor/Sponsor';
import type { SponsorableEntityType, SponsorshipRequest } from '../../domain/entities/SponsorshipRequest';
import type { SponsorRepository } from '../../domain/repositories/SponsorRepository';
import type { SponsorshipRequestRepository } from '../../domain/repositories/SponsorshipRequestRepository';
import { Money } from '../../domain/value-objects/Money';

export interface GetPendingSponsorshipRequestsInput {
  entityType: SponsorableEntityType;
  entityId: string;
}

export interface PendingSponsorshipRequestFinancials {
  offeredAmount: Money;
  platformFee: Money;
  netAmount: Money;
}

export interface PendingSponsorshipRequestSummary {
  request: SponsorshipRequest;
  sponsor: Sponsor | null;
  financials: PendingSponsorshipRequestFinancials;
}

export interface GetPendingSponsorshipRequestsResult {
  entityType: SponsorableEntityType;
  entityId: string;
  requests: PendingSponsorshipRequestSummary[];
  totalCount: number;
}

export type GetPendingSponsorshipRequestsErrorCode = 'REPOSITORY_ERROR';

export class GetPendingSponsorshipRequestsUseCase {
  constructor(
    private readonly sponsorshipRequestRepo: SponsorshipRequestRepository,
    private readonly sponsorRepo: SponsorRepository,
  ) {}

  async execute(
    input: GetPendingSponsorshipRequestsInput,
  ): Promise<
    Result<GetPendingSponsorshipRequestsResult, ApplicationErrorCode<GetPendingSponsorshipRequestsErrorCode, { message: string }>>
  > {
    try {
      const requests = await this.sponsorshipRequestRepo.findPendingByEntity(
        input.entityType,
        input.entityId,
      );

      const summaries: PendingSponsorshipRequestSummary[] = [];

      for (const request of requests) {
        const sponsor = await this.sponsorRepo.findById(request.sponsorId);

        const offeredAmount = Money.create(
          request.offeredAmount.amount,
          request.offeredAmount.currency,
        );
        const platformFee = request.getPlatformFee();
        const netAmount = request.getNetAmount();

        summaries.push({
          request,
          sponsor: sponsor ?? null,
          financials: {
            offeredAmount,
            platformFee,
            netAmount,
          },
        });
      }

      // Sort by creation date (newest first)
      summaries.sort((a, b) => b.request.createdAt.getTime() - a.request.createdAt.getTime());

      const result: GetPendingSponsorshipRequestsResult = {
        entityType: input.entityType,
        entityId: input.entityId,
        requests: summaries,
        totalCount: summaries.length,
      };

      return Result.ok(result);
    } catch (error) {
      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: {
          message:
            error instanceof Error
              ? error.message
              : 'Failed to fetch pending sponsorship requests',
        },
      });
    }
  }
}
