import {
  getAnalyticsConsent,
  hasAnalyticsConsent,
  isAnalyticsConsentResolved,
  isAnalyticsEnvironment,
  setAnalyticsConsent,
  ANALYTICS_CONSENT_CHANGED_EVENT,
  CONSENT_STORAGE_KEY,
} from './analyticsConsent';

describe('isAnalyticsEnvironment', () => {
  const originalEnv = process.env;

  afterEach(() => {
    process.env = { ...originalEnv };
  });

  it('returns true for production and beta deploy env', () => {
    process.env = { ...originalEnv, REACT_APP_DEPLOY_ENV: 'production' };
    expect(isAnalyticsEnvironment()).toBe(true);

    process.env = { ...originalEnv, REACT_APP_DEPLOY_ENV: 'beta' };
    expect(isAnalyticsEnvironment()).toBe(true);
  });

  it('returns false in development without deploy env', () => {
    process.env = {
      ...originalEnv,
      NODE_ENV: 'development',
      REACT_APP_DEPLOY_ENV: undefined,
      REACT_APP_TIANJI_WEBSITE_ID: undefined,
    };
    expect(isAnalyticsEnvironment()).toBe(false);
  });
});

describe('analytics consent storage', () => {
  const originalEnv = process.env;

  beforeEach(() => {
    localStorage.clear();
    process.env = { ...originalEnv, REACT_APP_DEPLOY_ENV: 'production' };
  });

  afterEach(() => {
    process.env = { ...originalEnv };
  });

  it('defaults to pending in production when unset', () => {
    expect(getAnalyticsConsent()).toBe('pending');
    expect(hasAnalyticsConsent()).toBe(false);
    expect(isAnalyticsConsentResolved()).toBe(false);
  });

  it('auto-grants consent outside analytics environments', () => {
    process.env = {
      ...originalEnv,
      NODE_ENV: 'development',
      REACT_APP_DEPLOY_ENV: undefined,
      REACT_APP_TIANJI_WEBSITE_ID: undefined,
    };
    expect(getAnalyticsConsent()).toBe('granted');
    expect(hasAnalyticsConsent()).toBe(true);
  });

  it('reads granted and denied from versioned localStorage key', () => {
    localStorage.setItem(CONSENT_STORAGE_KEY, 'granted');
    expect(getAnalyticsConsent()).toBe('granted');
    expect(isAnalyticsConsentResolved()).toBe(true);

    localStorage.setItem(CONSENT_STORAGE_KEY, 'denied');
    expect(getAnalyticsConsent()).toBe('denied');
    expect(hasAnalyticsConsent()).toBe(false);
  });

  it('migrates legacy consent key to v1', () => {
    localStorage.setItem('hesychia_analytics_consent', 'granted');
    expect(getAnalyticsConsent()).toBe('granted');
    expect(localStorage.getItem(CONSENT_STORAGE_KEY)).toBe('granted');
    expect(localStorage.getItem('hesychia_analytics_consent')).toBeNull();
  });

  it('persists consent and dispatches change event', () => {
    const listener = jest.fn();
    window.addEventListener(ANALYTICS_CONSENT_CHANGED_EVENT, listener);

    setAnalyticsConsent('denied');
    expect(localStorage.getItem(CONSENT_STORAGE_KEY)).toBe('denied');
    expect(listener).toHaveBeenCalled();

    setAnalyticsConsent('granted');
    expect(localStorage.getItem(CONSENT_STORAGE_KEY)).toBe('granted');

    window.removeEventListener(ANALYTICS_CONSENT_CHANGED_EVENT, listener);
  });
});
