import { describe, it, expect, vi, beforeEach } from 'vitest';
import { analyticsService } from './analyticsService';

describe('analyticsService', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('recordEvent', () => {
    it('should record an event successfully', async () => {
      const result = await analyticsService.recordEvent('test_event', {});

      expect(result).toBeUndefined(); // recordEvent retourne void
    });

    it('should handle different event types', async () => {
      const events = ['click', 'view', 'download', 'play'];

      for (const event of events) {
        await analyticsService.recordEvent(event, {});
      }

      // Si on arrive ici sans erreur, c'est bon
      expect(true).toBe(true);
    });
  });

  describe('getGlobalStats', () => {
    it('should return global statistics', async () => {
      const stats = await analyticsService.getGlobalStats();

      expect(stats).toBeDefined();
      expect(stats).toHaveProperty('total_users');
      expect(stats).toHaveProperty('total_tracks');
      expect(stats).toHaveProperty('total_plays');
    });

    it('should handle different time ranges', async () => {
      const ranges = ['7d', '30d', '90d', 'all'];

      for (const range of ranges) {
        const stats = await analyticsService.getGlobalStats(range);
        expect(stats).toBeDefined();
      }
    });
  });

  describe('getTopTracks', () => {
    it('should return top tracks', async () => {
      const tracks = await analyticsService.getTopTracks();

      expect(tracks).toBeDefined();
      expect(Array.isArray(tracks)).toBe(true);
    });

    it('should handle different time ranges', async () => {
      const ranges = ['7d', '30d', '90d', 'all'];

      for (const range of ranges) {
        const tracks = await analyticsService.getTopTracks(range);
        expect(Array.isArray(tracks)).toBe(true);
      }
    });
  });

  describe('getTrafficSources', () => {
    it('should return traffic sources', async () => {
      const sources = await analyticsService.getTrafficSources();

      expect(sources).toBeDefined();
      expect(Array.isArray(sources)).toBe(true);
    });
  });
});
