import { describe, expect, it } from "vitest";
import {
  DEFAULT_PROTOCOL_VERSION,
  DEFAULT_V3_FLAGS,
  V3_FLAG_ENV_KEYS,
  readV3Flags,
  resolveActiveVersion,
  type V3FeatureFlags,
} from "../src/version-flags";

const FLAG_KEYS = Object.keys(DEFAULT_V3_FLAGS) as (keyof V3FeatureFlags)[];

describe("V3 flags are default-off", () => {
  it("ships every flag disabled", () => {
    for (const key of FLAG_KEYS) {
      expect(DEFAULT_V3_FLAGS[key], key).toBe(false);
    }
  });

  it("reads an empty environment as fully disabled", () => {
    const { flags, configErrors } = readV3Flags({});
    expect(flags).toEqual(DEFAULT_V3_FLAGS);
    expect(configErrors).toEqual([]);
  });

  it("treats truthy-looking but non-exact values as disabled and reports them", () => {
    for (const raw of ["1", "TRUE", "True", "yes", "on", "enabled", " true"]) {
      const { flags, configErrors } = readV3Flags({
        [V3_FLAG_ENV_KEYS.v3MoneyMovementEnabled]: raw,
      });
      expect(flags.v3MoneyMovementEnabled, raw).toBe(false);
      expect(configErrors.length, raw).toBe(1);
    }
  });

  it("enables a flag only for the exact string \"true\"", () => {
    const { flags, configErrors } = readV3Flags({ [V3_FLAG_ENV_KEYS.v3UiEnabled]: "true" });
    expect(flags.v3UiEnabled).toBe(true);
    expect(flags.v3WritesEnabled).toBe(false);
    expect(flags.v3MoneyMovementEnabled).toBe(false);
    expect(configErrors).toEqual([]);
  });

  it("enabling one flag never cascades to the others", () => {
    const { flags } = readV3Flags({
      [V3_FLAG_ENV_KEYS.v3UiEnabled]: "true",
      [V3_FLAG_ENV_KEYS.v3ReadsEnabled]: "true",
    });
    expect(flags.v3WritesEnabled).toBe(false);
    expect(flags.v3MoneyMovementEnabled).toBe(false);
    expect(flags.v3AuditedPilotEnabled).toBe(false);
  });

  it("cannot be mutated by callers", () => {
    expect(Object.isFrozen(DEFAULT_V3_FLAGS)).toBe(true);
    expect(() => {
      (DEFAULT_V3_FLAGS as { v3WritesEnabled: boolean }).v3WritesEnabled = true;
    }).toThrow();
    expect(DEFAULT_V3_FLAGS.v3WritesEnabled).toBe(false);
  });
});

describe("no user is defaulted into V3", () => {
  it("defaults to V2", () => {
    expect(DEFAULT_PROTOCOL_VERSION).toBe("v2");
    expect(resolveActiveVersion()).toBe("v2");
    expect(resolveActiveVersion({}, DEFAULT_V3_FLAGS)).toBe("v2");
  });

  it("stays on V2 when every V3 flag is on but the user selected nothing", () => {
    const allOn: V3FeatureFlags = {
      v3UiEnabled: true,
      v3ReadsEnabled: true,
      v3WritesEnabled: true,
      v3MoneyMovementEnabled: true,
      v3AuditedPilotEnabled: true,
    };
    expect(resolveActiveVersion({}, allOn)).toBe("v2");
    expect(resolveActiveVersion({ selectedVersion: "v2" }, allOn)).toBe("v2");
  });

  it("stays on V2 when the user selected V3 but the UI flag is off", () => {
    expect(resolveActiveVersion({ selectedVersion: "v3" }, DEFAULT_V3_FLAGS)).toBe("v2");
  });

  it("returns V3 only for explicit selection plus an enabled UI flag", () => {
    const flags = { ...DEFAULT_V3_FLAGS, v3UiEnabled: true };
    expect(resolveActiveVersion({ selectedVersion: "v3" }, flags)).toBe("v3");
  });
});
