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

describe('Environment Variables', () => {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const originalEnv = import.meta.env;

  beforeEach(() => {
    // Reset import.meta.env
    vi.resetModules();
  });

  it('should have default values when env variables are not set', () => {
    // Mock empty env
    Object.defineProperty(import.meta, 'env', {
      value: {},
      writable: true,
    });

    // Need to re-import after mocking
    vi.resetModules();
    const { env: testEnv } = require('./env');

    expect(testEnv.API_URL).toBe('http://localhost:8080/api/v1');
    expect(testEnv.WS_URL).toBe('ws://localhost:8081/ws');
    expect(testEnv.STREAM_URL).toBe('http://localhost:8082');
    expect(testEnv.UPLOAD_URL).toBe('http://localhost:8080/upload');
    expect(testEnv.APP_NAME).toBe('Veza');
    expect(testEnv.DEBUG).toBe(false);
    expect(testEnv.USE_MSW).toBe(false);
  });

  it('should parse environment variables correctly', () => {
    Object.defineProperty(import.meta, 'env', {
      value: {
        VITE_API_URL: 'https://api.example.com/v1',
        VITE_WS_URL: 'wss://ws.example.com/ws',
        VITE_STREAM_URL: 'https://stream.example.com/stream',
        VITE_UPLOAD_URL: 'https://upload.example.com',
        VITE_APP_NAME: 'Test App',
        VITE_DEBUG: 'true',
        VITE_USE_MSW: '1',
      },
      writable: true,
    });

    vi.resetModules();
    const { env: testEnv } = require('./env');

    expect(testEnv.API_URL).toBe('https://api.example.com/v1');
    expect(testEnv.WS_URL).toBe('wss://ws.example.com');
    expect(testEnv.STREAM_URL).toBe('https://stream.example.com');
    expect(testEnv.UPLOAD_URL).toBe('https://upload.example.com');
    expect(testEnv.APP_NAME).toBe('Test App');
    expect(testEnv.DEBUG).toBe(true);
    expect(testEnv.USE_MSW).toBe(true);
  });

  it('should parse DEBUG as boolean correctly', () => {
    Object.defineProperty(import.meta, 'env', {
      value: {
        VITE_DEBUG: 'true',
      },
      writable: true,
    });

    vi.resetModules();
    const { env: testEnv } = require('./env');

    expect(testEnv.DEBUG).toBe(true);

    Object.defineProperty(import.meta, 'env', {
      value: {
        VITE_DEBUG: 'false',
      },
      writable: true,
    });

    vi.resetModules();
    const { env: testEnv2 } = require('./env');
    expect(testEnv2.DEBUG).toBe(false);
  });

  it('should parse USE_MSW as boolean correctly', () => {
    Object.defineProperty(import.meta, 'env', {
      value: {
        VITE_USE_MSW: '1',
      },
      writable: true,
    });

    vi.resetModules();
    const { env: testEnv } = require('./env');

    expect(testEnv.USE_MSW).toBe(true);

    Object.defineProperty(import.meta, 'env', {
      value: {
        VITE_USE_MSW: '0',
      },
      writable: true,
    });

    vi.resetModules();
    const { env: testEnv2 } = require('./env');
    expect(testEnv2.USE_MSW).toBe(false);
  });

  it('should export env object with correct structure', () => {
    expect(env).toHaveProperty('API_URL');
    expect(env).toHaveProperty('WS_URL');
    expect(env).toHaveProperty('STREAM_URL');
    expect(env).toHaveProperty('UPLOAD_URL');
    expect(env).toHaveProperty('APP_NAME');
    expect(env).toHaveProperty('DEBUG');
    expect(env).toHaveProperty('USE_MSW');
    expect(env).toHaveProperty('FCM_VAPID_KEY');
  });

  it('should have correct types for env values', () => {
    expect(typeof env.API_URL).toBe('string');
    expect(typeof env.WS_URL).toBe('string');
    expect(typeof env.STREAM_URL).toBe('string');
    expect(typeof env.UPLOAD_URL).toBe('string');
    expect(typeof env.APP_NAME).toBe('string');
    expect(typeof env.DEBUG).toBe('boolean');
    expect(typeof env.USE_MSW).toBe('boolean');
  });
});
