/**
 * Pure predicates for the multi-profile gate (Base+), issue #300.
 *
 * NON-destructive by construction: these functions only decide whether a UI
 * surface is locked — nothing is ever removed from profiles.json. An upgrade
 * (edition becomes base/premium) flips `allowed` and every profile reappears
 * untouched, without any migration.
 *
 * Anti-flash rule: the gate is active only when the license is `ready`
 * (`status === "ready"` in LicenseContext). While loading or errored, nothing
 * is locked — a paying user must never see a "locked" flash at boot (the same
 * rule RequireFeature/Sidebar follow).
 */

/** Shape returned by `useEntitlement("multi-profile")`. */
export interface EntitlementGate {
  allowed: boolean;
  ready: boolean;
}

/**
 * Is switching to `profileId` locked?
 *
 * Locked iff the gate is active (ready && !allowed) AND the profile is beyond
 * the active one. A Free user keeps full access to their active profile.
 *
 * `activeProfileId === null` (degenerate state: no active profile resolves)
 * locks NOTHING — we never lock a user out of all of their profiles; the only
 * surface without an active profile is ProfileSelectionPage, where selection
 * stays free by design (see decisions log #300).
 */
export function isProfileSwitchLocked(
  profileId: string,
  activeProfileId: string | null,
  gate: EntitlementGate,
): boolean {
  if (!gate.ready || gate.allowed) return false;
  if (activeProfileId === null) return false;
  return profileId !== activeProfileId;
}

/**
 * Is creating a NEW profile locked?
 *
 * Locked iff the gate is active AND at least one profile already exists — a
 * Free user is entitled to one profile, so creation from an empty config
 * (fresh/repaired install) is never blocked.
 */
export function isProfileCreationLocked(
  profileCount: number,
  gate: EntitlementGate,
): boolean {
  if (!gate.ready || gate.allowed) return false;
  return profileCount >= 1;
}
