import { describe, it, expect, vi, Mocked } from 'vitest';
import { MembershipFeeService } from './MembershipFeeService';
import { PaymentsApiClient } from '../../api/payments/PaymentsApiClient';
import { MembershipFeeViewModel } from '../../view-models';
import type { MembershipFeeDto } from '../../types/generated';

describe('MembershipFeeService', () => {
  let mockApiClient: Mocked<PaymentsApiClient>;
  let service: MembershipFeeService;

  beforeEach(() => {
    mockApiClient = {
      getMembershipFees: vi.fn(),
    } as Mocked<PaymentsApiClient>;

    service = new MembershipFeeService(mockApiClient);
  });

  describe('getMembershipFees', () => {
    it('should call apiClient.getMembershipFees with correct leagueId and return fee and payments', async () => {
      const leagueId = 'league-123';
      const mockFee: MembershipFeeDto = { id: 'fee-1', leagueId: 'league-123', seasonId: undefined, type: 'season', amount: 100, enabled: true, createdAt: new Date(), updatedAt: new Date() };
      const mockPayments: any[] = [];
      const mockOutput = { fee: mockFee, payments: mockPayments };
      mockApiClient.getMembershipFees.mockResolvedValue(mockOutput);

      const result = await service.getMembershipFees(leagueId);

      expect(mockApiClient.getMembershipFees).toHaveBeenCalledWith({ leagueId });
      expect(result.fee).toBeInstanceOf(MembershipFeeViewModel);
      expect(result.fee!.id).toEqual('fee-1');
      expect(result.payments).toEqual([]);
    });

    it('should return null fee when no fee is returned', async () => {
      const leagueId = 'league-456';
      const mockOutput = { fee: null, payments: [] };
      mockApiClient.getMembershipFees.mockResolvedValue(mockOutput);

      const result = await service.getMembershipFees(leagueId);

      expect(mockApiClient.getMembershipFees).toHaveBeenCalledWith({ leagueId });
      expect(result.fee).toBeNull();
    });
  });
});