import type { PaymentsApiClient } from '../../api/payments/PaymentsApiClient';
import type { PaymentListPresenter } from '../../presenters/PaymentListPresenter';
import type {
  CreatePaymentInputDto,
  CreatePaymentOutputDto,
} from '../../dtos';
import type {
  PaymentViewModel,
  MembershipFeeViewModel,
  PrizeViewModel,
  WalletViewModel,
} from '../../view-models';

/**
 * Payment Service
 *
 * Orchestrates payment operations by coordinating API calls and presentation logic.
 * All dependencies are injected via constructor.
 */
export class PaymentService {
  constructor(
    private readonly apiClient: PaymentsApiClient,
    private readonly paymentListPresenter: PaymentListPresenter,
    private readonly presentPayment: (dto: any) => PaymentViewModel,
    private readonly presentMembershipFee: (dto: any) => MembershipFeeViewModel,
    private readonly presentPrize: (dto: any) => PrizeViewModel,
    private readonly presentWallet: (dto: any) => WalletViewModel
  ) {}

  /**
   * Get all payments with optional filters
   */
  async getPayments(leagueId?: string, driverId?: string): Promise<PaymentViewModel[]> {
    try {
      const dto = await this.apiClient.getPayments(leagueId, driverId);
      return this.paymentListPresenter.present(dto);
    } catch (error) {
      throw error;
    }
  }

  /**
   * Get single payment by ID
   */
  async getPayment(paymentId: string): Promise<PaymentViewModel> {
    try {
      // Note: Assuming the API returns a single payment from the list
      const dto = await this.apiClient.getPayments();
      const payment = dto.payments.find(p => p.id === paymentId);
      if (!payment) {
        throw new Error(`Payment with ID ${paymentId} not found`);
      }
      return this.presentPayment(payment);
    } catch (error) {
      throw error;
    }
  }

  /**
   * Create a new payment
   */
  async createPayment(input: CreatePaymentInputDto): Promise<CreatePaymentOutputDto> {
    try {
      return await this.apiClient.createPayment(input);
    } catch (error) {
      throw error;
    }
  }

  /**
   * Get membership fees for a league
   */
  async getMembershipFees(leagueId: string): Promise<MembershipFeeViewModel[]> {
    try {
      const dto = await this.apiClient.getMembershipFees(leagueId);
      return dto.fees.map(fee => this.presentMembershipFee(fee));
    } catch (error) {
      throw error;
    }
  }

  /**
   * Get prizes with optional filters
   */
  async getPrizes(leagueId?: string, seasonId?: string): Promise<PrizeViewModel[]> {
    try {
      const dto = await this.apiClient.getPrizes(leagueId, seasonId);
      return dto.prizes.map(prize => this.presentPrize(prize));
    } catch (error) {
      throw error;
    }
  }

  /**
   * Get wallet for a driver
   */
  async getWallet(driverId: string): Promise<WalletViewModel> {
    try {
      const dto = await this.apiClient.getWallet(driverId);
      return this.presentWallet(dto);
    } catch (error) {
      throw error;
    }
  }

  /**
   * Process a payment (alias for createPayment)
   */
  async processPayment(input: CreatePaymentInputDto): Promise<CreatePaymentOutputDto> {
    try {
      return await this.createPayment(input);
    } catch (error) {
      throw error;
    }
  }

  /**
   * Get payment history for a user (driver)
   */
  async getPaymentHistory(driverId: string): Promise<PaymentViewModel[]> {
    try {
      return await this.getPayments(undefined, driverId);
    } catch (error) {
      throw error;
    }
  }
}