import { describe, it, expect } from "vitest";
import {
  isProfileSwitchLocked,
  isProfileCreationLocked,
  type EntitlementGate,
} from "./profileGate";

const LOCKED_GATE: EntitlementGate = { allowed: false, ready: true };
const ALLOWED_GATE: EntitlementGate = { allowed: true, ready: true };
const BOOTING_GATE: EntitlementGate = { allowed: false, ready: false };

describe("isProfileSwitchLocked", () => {
  it("locks profiles beyond the active one when gate is active (Free, ready)", () => {
    expect(isProfileSwitchLocked("p2", "p1", LOCKED_GATE)).toBe(true);
    expect(isProfileSwitchLocked("p3", "p1", LOCKED_GATE)).toBe(true);
  });

  it("keeps the active profile accessible for a Free user", () => {
    expect(isProfileSwitchLocked("p1", "p1", LOCKED_GATE)).toBe(false);
  });

  it("locks nothing while the license is not ready (anti-flash at boot)", () => {
    expect(isProfileSwitchLocked("p2", "p1", BOOTING_GATE)).toBe(false);
    expect(isProfileSwitchLocked("p1", "p1", BOOTING_GATE)).toBe(false);
  });

  it("locks nothing when multi-profile is allowed (Base/Premium)", () => {
    expect(isProfileSwitchLocked("p2", "p1", ALLOWED_GATE)).toBe(false);
  });

  it("locks nothing when no active profile resolves (never lock out of ALL profiles)", () => {
    expect(isProfileSwitchLocked("p1", null, LOCKED_GATE)).toBe(false);
    expect(isProfileSwitchLocked("p2", null, LOCKED_GATE)).toBe(false);
  });

  it("stays unlocked when allowed even if not ready (allowed short-circuits)", () => {
    expect(isProfileSwitchLocked("p2", "p1", { allowed: true, ready: false })).toBe(false);
  });
});

describe("isProfileCreationLocked", () => {
  it("locks creation for a Free user who already has at least one profile", () => {
    expect(isProfileCreationLocked(1, LOCKED_GATE)).toBe(true);
    expect(isProfileCreationLocked(5, LOCKED_GATE)).toBe(true);
  });

  it("never blocks creating the FIRST profile (empty config)", () => {
    expect(isProfileCreationLocked(0, LOCKED_GATE)).toBe(false);
  });

  it("locks nothing while the license is not ready (anti-flash at boot)", () => {
    expect(isProfileCreationLocked(3, BOOTING_GATE)).toBe(false);
  });

  it("locks nothing when multi-profile is allowed (Base/Premium)", () => {
    expect(isProfileCreationLocked(3, ALLOWED_GATE)).toBe(false);
    expect(isProfileCreationLocked(0, ALLOWED_GATE)).toBe(false);
  });
});
