import { describe, it, expect } from 'vitest';
import { EventBatcher } from './EventBatcher';
import { ConsentManager } from '../consent/ConsentManager';
import { AnalyticsManager } from './AnalyticsManager';

describe('Analytics System', () => {
  describe('EventBatcher', () => {
    it('should create batcher with default config', () => {
      const batcher = new EventBatcher();
      expect(batcher.batchSize).toBe(10);
      expect(batcher.batchTimeout).toBe(30000);
    });

    it('should create batcher with custom config', () => {
      const batcher = new EventBatcher({
        batchSize: 5,
        batchTimeout: 5000,
      });
      expect(batcher.batchSize).toBe(5);
      expect(batcher.batchTimeout).toBe(5000);
    });

    it('should track pending count', () => {
      const batcher = new EventBatcher();
      expect(batcher.getPendingCount()).toBe(0);

      batcher.add({ eventName: 'test' });
      expect(batcher.getPendingCount()).toBe(1);

      batcher.destroy();
    });

    it('should not add null events', () => {
      const batcher = new EventBatcher();
      batcher.add(null);
      batcher.add(undefined);
      expect(batcher.getPendingCount()).toBe(0);
    });
  });

  describe('ConsentManager', () => {
    beforeEach(() => {
      localStorage.clear();
    });

    it('should manage consent state', () => {
      ConsentManager.setConsent(true);
      expect(ConsentManager.hasConsent()).toBe(true);
      expect(ConsentManager.hasUserDecided()).toBe(true);
    });

    it('should return false when consent not set', () => {
      expect(ConsentManager.hasConsent()).toBe(false);
      expect(ConsentManager.hasUserDecided()).toBe(false);
    });

    it('should reset consent', () => {
      ConsentManager.setConsent(true);
      ConsentManager.reset();
      expect(ConsentManager.hasUserDecided()).toBe(false);
    });

    it('should store and retrieve consent', () => {
      ConsentManager.setConsent(true);
      const consent = ConsentManager.getConsent();
      expect(consent.accepted).toBe(true);
      expect(consent.version).toBe('1');
    });
  });

  describe('AnalyticsManager', () => {
    it('should create with noop provider by default', () => {
      const manager = new AnalyticsManager();
      expect(manager.provider.constructor.name).toBe('NoopProvider');
    });

    it('should create with GA4Provider', () => {
      const manager = new AnalyticsManager({
        providerType: 'ga4',
        measurementId: 'G-TEST123',
      });
      expect(manager.provider.constructor.name).toBe('GA4Provider');
    });

    it('should fallback to noop for unknown provider', () => {
      const manager = new AnalyticsManager({
        providerType: 'unknown',
      });
      expect(manager.provider.constructor.name).toBe('NoopProvider');
    });

    it('should have network detector', () => {
      const manager = new AnalyticsManager();
      expect(manager.networkDetector).toBeDefined();
      expect(typeof manager.networkDetector.getStatus).toBe('function');
    });
  });
});
