/**
 * Application Use Case: GetPaymentsUseCase
 *
 * Retrieves payments based on filters.
 */

import type { UseCase } from '@core/shared/application/UseCase';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Payment, PaymentType } from '../../domain/entities/Payment';
import type { PaymentRepository } from '../../domain/repositories/PaymentRepository';

export interface GetPaymentsInput {
  leagueId?: string;
  payerId?: string;
  type?: PaymentType;
}

export interface GetPaymentsResult {
  payments: Payment[];
}

export type GetPaymentsErrorCode = never;

export class GetPaymentsUseCase
  implements UseCase<GetPaymentsInput, GetPaymentsResult, GetPaymentsErrorCode>
{
  constructor(
    private readonly paymentRepository: PaymentRepository,
  ) {}

  async execute(input: GetPaymentsInput): Promise<Result<GetPaymentsResult, ApplicationErrorCode<GetPaymentsErrorCode>>> {
    const { leagueId, payerId, type } = input;

    const filters: { leagueId?: string; payerId?: string; type?: PaymentType } = {};
    if (leagueId !== undefined) filters.leagueId = leagueId;
    if (payerId !== undefined) filters.payerId = payerId;
    if (type !== undefined) filters.type = type;

    const payments = await this.paymentRepository.findByFilters(filters);

    return Result.ok({ payments });
  }
}