import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SponsorService } from './SponsorService';
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
import { SponsorViewModel } from '@/lib/view-models/SponsorViewModel';

// Mock the API client
vi.mock('@/lib/api/sponsors/SponsorsApiClient');

describe('SponsorService', () => {
  let service: SponsorService;
  let mockApiClientInstance: any;

  beforeEach(() => {
    process.env.API_BASE_URL = 'http://localhost:3001';

    vi.clearAllMocks();
    service = new SponsorService();
    // @ts-ignore - accessing private property for testing
    mockApiClientInstance = service.apiClient;
  });

  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',
          },
        ],
      };

      mockApiClientInstance.getAll.mockResolvedValue(mockDto);

      const result = await service.getAllSponsors();

      expect(mockApiClientInstance.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);
    });
  });

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

      mockApiClientInstance.getDashboard.mockResolvedValue(mockDto as any);

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

      expect(mockApiClientInstance.getDashboard).toHaveBeenCalledWith('sponsor-1');
      expect(result.isOk()).toBe(true);
      expect(result.unwrap().sponsorId).toBe('sponsor-1');
      expect(result.unwrap().sponsorName).toBe('Test Sponsor');
    });

    it('should return Result.err with type "notFound" when apiClient.getDashboard returns null', async () => {
      mockApiClientInstance.getDashboard.mockResolvedValue(null);

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

      expect(mockApiClientInstance.getDashboard).toHaveBeenCalledWith('sponsor-1');
      expect(result.isErr()).toBe(true);
      expect(result.getError().type).toBe('notFound');
    });

    it('should return Result.err with type "serverError" when apiClient.getDashboard fails', async () => {
      const error = new Error('API call failed');
      mockApiClientInstance.getDashboard.mockRejectedValue(error);

      const result = await service.getSponsorDashboard('sponsor-1');
      expect(result.isErr()).toBe(true);
      expect(result.getError().type).toBe('serverError');
    });
  });

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

      mockApiClientInstance.getSponsorships.mockResolvedValue(mockDto as any);

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

      expect(mockApiClientInstance.getSponsorships).toHaveBeenCalledWith('sponsor-1');
      expect(result.isOk()).toBe(true);
      expect(result.unwrap().sponsorId).toBe('sponsor-1');
      expect(result.unwrap().sponsorName).toBe('Test Sponsor');
    });

    it('should return Result.err with type "notFound" when apiClient.getSponsorships returns null', async () => {
      mockApiClientInstance.getSponsorships.mockResolvedValue(null);

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

      expect(mockApiClientInstance.getSponsorships).toHaveBeenCalledWith('sponsor-1');
      expect(result.isErr()).toBe(true);
      expect(result.getError().type).toBe('notFound');
    });

    it('should return Result.err with type "serverError" when apiClient.getSponsorships fails', async () => {
      const error = new Error('API call failed');
      mockApiClientInstance.getSponsorships.mockRejectedValue(error);

      const result = await service.getSponsorSponsorships('sponsor-1');
      expect(result.isErr()).toBe(true);
      expect(result.getError().type).toBe('serverError');
    });
  });
});