import { afterEach, beforeEach, describe, expect, it } from "vitest";

describe("getEnv", () => {
  const originalEnv = { ...process.env };

  beforeEach(() => {
    // Clear env to a clean slate, then set a minimum valid shape.
    for (const key of Object.keys(process.env)) {
      if (key.startsWith("VITEST_")) continue;
      delete process.env[key];
    }
    process.env["NODE_ENV"] = "testing";
    process.env["BUILD_TARGET"] = "standalone";
    process.env["PROD_ORIGIN"] = "https://flights.aeroflot.ru";
    process.env["API_BASE_URL"] = "https://platform.aeroflot.ru";
    process.env["SIGNALR_HUB_URL"] = "wss://platform.aeroflot.ru/hub";
    process.env["ANALYTICS_METRICA"] = "true";
    process.env["ANALYTICS_CTM"] = "false";
    process.env["ANALYTICS_VARIOCUBE"] = "false";
    process.env["ANALYTICS_DYNATRACE"] = "true";
    process.env["VERSION"] = "abc1234";
  });

  afterEach(async () => {
    process.env = { ...originalEnv };
    // Reset the module cache so getEnv re-reads the updated env.
    const mod = await import("./index.js");
    mod.__resetEnvCacheForTests();
  });

  it("returns a typed Env object for valid input", async () => {
    const { getEnv } = await import("./index.js");
    const env = getEnv();
    expect(env.NODE_ENV).toBe("testing");
    expect(env.BUILD_TARGET).toBe("standalone");
    expect(env.PROD_ORIGIN).toBe("https://flights.aeroflot.ru");
    expect(env.API_BASE_URL).toBe("https://platform.aeroflot.ru");
    expect(env.SIGNALR_HUB_URL).toBe("wss://platform.aeroflot.ru/hub");
    expect(env.ANALYTICS_ENABLED).toEqual({
      metrica: true,
      ctm: false,
      variocube: false,
      dynatrace: true,
    });
    expect(env.VERSION).toBe("abc1234");
    expect(env.OTEL_EXPORTER_OTLP_ENDPOINT).toBeUndefined();
  });

  it("caches the result across calls (same object identity)", async () => {
    const { getEnv } = await import("./index.js");
    const a = getEnv();
    const b = getEnv();
    expect(a).toBe(b);
  });

  it("uses default value when a field is missing (browser-safe defaults)", async () => {
    delete process.env["API_BASE_URL"];
    const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
    __resetEnvCacheForTests();
    const env = getEnv();
    // API_BASE_URL defaults to the same-origin proxy path; the proxy
    // forwards to https://flights.test.aeroflot.ru (see scripts/dev-server.mjs).
    expect(env.API_BASE_URL).toBe("http://localhost:8080/api");
  });

  it("throws when NODE_ENV is not one of the allowed values", async () => {
    process.env["NODE_ENV"] = "banana";
    const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
    __resetEnvCacheForTests();
    expect(() => getEnv()).toThrow(/NODE_ENV/);
  });

  it("throws when BUILD_TARGET is neither standalone nor remote", async () => {
    process.env["BUILD_TARGET"] = "hybrid";
    const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
    __resetEnvCacheForTests();
    expect(() => getEnv()).toThrow(/BUILD_TARGET/);
  });

  it("accepts optional OTEL_EXPORTER_OTLP_ENDPOINT when provided", async () => {
    process.env["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://otel.example/v1/traces";
    const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
    __resetEnvCacheForTests();
    const env = getEnv();
    expect(env.OTEL_EXPORTER_OTLP_ENDPOINT).toBe("https://otel.example/v1/traces");
  });

  it("rejects OTEL_EXPORTER_OTLP_ENDPOINT that is not a URL", async () => {
    process.env["OTEL_EXPORTER_OTLP_ENDPOINT"] = "not-a-url";
    const { getEnv, __resetEnvCacheForTests } = await import("./index.js");
    __resetEnvCacheForTests();
    expect(() => getEnv()).toThrow(/OTEL_EXPORTER_OTLP_ENDPOINT/);
  });
});
