import { describe, it, expect, vi, Mocked } from 'vitest';
import { SponsorService } from './SponsorService';
import { SponsorsApiClient } from '../../api/sponsors/SponsorsApiClient';
import { SponsorViewModel } from '../../view-models/SponsorViewModel';
import { SponsorDashboardViewModel } from '../../view-models/SponsorDashboardViewModel';
import { SponsorSponsorshipsViewModel } from '../../view-models/SponsorSponsorshipsViewModel';

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

  beforeEach(() => {
    mockApiClient = {
      getAll: vi.fn(),
      getDashboard: vi.fn(),
      getSponsorships: vi.fn(),
      create: vi.fn(),
      getPricing: vi.fn(),
      getSponsor: vi.fn(),
      getPendingSponsorshipRequests: vi.fn(),
      acceptSponsorshipRequest: vi.fn(),
      rejectSponsorshipRequest: vi.fn(),
    } as Mocked<SponsorsApiClient>;

    service = new SponsorService(mockApiClient);
  });

  describe('getAllSponsors', () => {
    it('should call apiClient.getAll and return array of SponsorViewModel', async () => {
      const mockDto = {
        sponsors: [
          {
            id: 'sponsor-1',
            name: 'Test Sponsor',
            logoUrl: 'https://example.com/logo.png',
            websiteUrl: 'https://example.com',
          },
        ],
      };

      mockApiClient.getAll.mockResolvedValue(mockDto);

      const result = await service.getAllSponsors();

      expect(mockApiClient.getAll).toHaveBeenCalled();
      expect(result).toHaveLength(1);
      expect(result[0]).toBeInstanceOf(SponsorViewModel);
      expect(result[0].id).toBe('sponsor-1');
      expect(result[0].name).toBe('Test Sponsor');
      expect(result[0].hasWebsite).toBe(true);
    });

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

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

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

      mockApiClient.getDashboard.mockResolvedValue(mockDto);

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

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

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

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

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

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

      await expect(service.getSponsorDashboard('sponsor-1')).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');
    });
  });

  describe('createSponsor', () => {
    it('should call apiClient.create and return the result', async () => {
      const input = {
        name: 'New Sponsor',
      };

      const mockOutput = {
        id: 'sponsor-123',
        name: 'New Sponsor',
      };

      mockApiClient.create.mockResolvedValue(mockOutput);

      const result = await service.createSponsor(input);

      expect(mockApiClient.create).toHaveBeenCalledWith(input);
      expect(result).toEqual(mockOutput);
    });

    it('should throw error when apiClient.create fails', async () => {
      const input = {
        name: 'New Sponsor',
      };

      const error = new Error('API call failed');
      mockApiClient.create.mockRejectedValue(error);

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

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

      mockApiClient.getPricing.mockResolvedValue(mockPricing);

      const result = await service.getSponsorshipPricing();

      expect(mockApiClient.getPricing).toHaveBeenCalled();
      expect(result).toEqual(mockPricing);
    });

    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');
    });
  });
});