import { describe, it, expect, vi, Mocked } from 'vitest';
import { DashboardService } from './DashboardService';
import { AnalyticsApiClient } from '@/lib/api/analytics/AnalyticsApiClient';
import { AnalyticsDashboardViewModel, AnalyticsMetricsViewModel } from '../../view-models';

describe('DashboardService', () => {
  let mockApiClient: Mocked<AnalyticsApiClient>;
  let service: DashboardService;

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

    mockApiClient = {
      getDashboardOverview: vi.fn(),
      getAnalyticsMetrics: vi.fn(),
    } as any;

    service = new DashboardService();
    (service as any).apiClient = mockApiClient;
    (service as any).analyticsApiClient = mockApiClient;
  });

  describe('getDashboardOverview', () => {
    it('should call apiClient.getDashboardOverview and return Result with DashboardOverviewDTO', async () => {
      const dto = {
        totalUsers: 100,
        activeUsers: 50,
        totalRaces: 20,
        totalLeagues: 5,
      };
      mockApiClient.getDashboardOverview.mockResolvedValue(dto);

      const result = await service.getDashboardOverview();

      expect(mockApiClient.getDashboardOverview).toHaveBeenCalled();
      expect(result.isOk()).toBe(true);
      const value = (result as any).value;
      expect(value.totalUsers).toBe(100);
      expect(value.activeUsers).toBe(50);
      expect(value.totalRaces).toBe(20);
      expect(value.totalLeagues).toBe(5);
    });
  });

  describe('getAnalyticsMetrics', () => {
    it('should call apiClient.getAnalyticsMetrics and return Result with AnalyticsMetricsViewModel', async () => {
      const dto = {
        pageViews: 1000,
        uniqueVisitors: 500,
        averageSessionDuration: 300,
        bounceRate: 0.25,
      };
      mockApiClient.getAnalyticsMetrics.mockResolvedValue(dto);

      const result = await service.getAnalyticsMetrics();

      expect(mockApiClient.getAnalyticsMetrics).toHaveBeenCalled();
      expect(result.isOk()).toBe(true);
      const value = (result as any).value;
      expect(value.pageViews).toBe(1000);
      expect(value.uniqueVisitors).toBe(500);
      expect(value.averageSessionDuration).toBe(300);
      expect(value.bounceRate).toBe(0.25);
    });
  });

});
