import { describe, it, expect, vi, Mocked } from 'vitest';
import { SponsorshipService } from './SponsorshipService';
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
import { SponsorshipPricingViewModel } from '@/lib/view-models/SponsorshipPricingViewModel';
import { SponsorSponsorshipsViewModel } from '@/lib/view-models/SponsorSponsorshipsViewModel';

describe('SponsorshipService', () => {
  let mockApiClient: Mocked<SponsorsApiClient>;
  let service: SponsorshipService;

  beforeEach(() => {
    mockApiClient = {
      getPricing: vi.fn(),
      getSponsorships: vi.fn(),
    } as Mocked<SponsorsApiClient>;

    service = new SponsorshipService(mockApiClient);
  });

  describe('getSponsorshipPricing', () => {
    it('should call apiClient.getPricing and return SponsorshipPricingViewModel', async () => {
      const mockDto = {
        pricing: [
          { entityType: 'league', price: 100 },
          { entityType: 'driver', price: 50 },
        ],
      };

      mockApiClient.getPricing.mockResolvedValue(mockDto);

      const result = await service.getSponsorshipPricing();

      expect(mockApiClient.getPricing).toHaveBeenCalled();
      expect(result).toBeInstanceOf(SponsorshipPricingViewModel);
      expect(result.mainSlotPrice).toBe(100);
      expect(result.secondarySlotPrice).toBe(50);
      expect(result.currency).toBe('USD');
    });

    it('should handle missing entity types with default prices', async () => {
      const mockDto = {
        pricing: [],
      };

      mockApiClient.getPricing.mockResolvedValue(mockDto);

      const result = await service.getSponsorshipPricing();

      expect(result.mainSlotPrice).toBe(0);
      expect(result.secondarySlotPrice).toBe(0);
      expect(result.currency).toBe('USD');
    });

    it('should throw error when apiClient.getPricing fails', async () => {
      const error = new Error('API call failed');
      mockApiClient.getPricing.mockRejectedValue(error);

      await expect(service.getSponsorshipPricing()).rejects.toThrow('API call failed');
    });
  });

  describe('getSponsorSponsorships', () => {
    it('should call apiClient.getSponsorships and return SponsorSponsorshipsViewModel when data exists', async () => {
      const mockDto = {
        sponsorId: 'sponsor-1',
        sponsorName: 'Test Sponsor',
      };

      mockApiClient.getSponsorships.mockResolvedValue(mockDto);

      const result = await service.getSponsorSponsorships('sponsor-1');

      expect(mockApiClient.getSponsorships).toHaveBeenCalledWith('sponsor-1');
      expect(result).toBeInstanceOf(SponsorSponsorshipsViewModel);
      expect(result?.sponsorId).toBe('sponsor-1');
      expect(result?.sponsorName).toBe('Test Sponsor');
    });

    it('should return null when apiClient.getSponsorships returns null', async () => {
      mockApiClient.getSponsorships.mockResolvedValue(null);

      const result = await service.getSponsorSponsorships('sponsor-1');

      expect(mockApiClient.getSponsorships).toHaveBeenCalledWith('sponsor-1');
      expect(result).toBeNull();
    });

    it('should throw error when apiClient.getSponsorships fails', async () => {
      const error = new Error('API call failed');
      mockApiClient.getSponsorships.mockRejectedValue(error);

      await expect(service.getSponsorSponsorships('sponsor-1')).rejects.toThrow('API call failed');
    });
  });
});