import { describe, it, expect, vi, type Mock } from 'vitest';
import { CreatePaymentUseCase, type CreatePaymentInput } from './CreatePaymentUseCase';
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
import { PaymentType, PayerType } from '../../domain/entities/Payment';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';

describe('CreatePaymentUseCase', () => {
  let paymentRepository: {
    create: Mock;
  };
  let output: {
    present: Mock;
  };
  let useCase: CreatePaymentUseCase;

  beforeEach(() => {
    paymentRepository = {
      create: vi.fn(),
    };

    output = {
      present: vi.fn(),
    };

    useCase = new CreatePaymentUseCase(
      paymentRepository as unknown as IPaymentRepository,
      output as unknown as UseCaseOutputPort<unknown>,
    );
  });

  it('creates a payment and presents the result', async () => {
    const input: CreatePaymentInput = {
      type: PaymentType.SPONSORSHIP,
      amount: 100,
      payerId: 'payer-1',
      payerType: PayerType.SPONSOR,
      leagueId: 'league-1',
      seasonId: 'season-1',
    };

    const createdPayment = {
      id: 'payment-123',
      type: PaymentType.SPONSORSHIP,
      amount: 100,
      platformFee: 5,
      netAmount: 95,
      payerId: 'payer-1',
      payerType: PayerType.SPONSOR,
      leagueId: 'league-1',
      seasonId: 'season-1',
      status: 'pending',
      createdAt: new Date(),
      completedAt: undefined,
    };

    paymentRepository.create.mockResolvedValue(createdPayment);

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(paymentRepository.create).toHaveBeenCalledWith({
      id: expect.stringContaining('payment-'),
      type: 'sponsorship',
      amount: 100,
      platformFee: 5,
      netAmount: 95,
      payerId: 'payer-1',
      payerType: 'sponsor',
      leagueId: 'league-1',
      seasonId: 'season-1',
      status: 'pending',
      createdAt: expect.any(Date),
    });
    expect(output.present).toHaveBeenCalledWith({ payment: createdPayment });
  });
});