import type { SFPreferences } from "../preferences.js";
import { loadEffectiveSFPreferences } from "../preferences.js";

export interface UokFlags {
  enabled: boolean;
  legacyFallback: boolean;
  gates: boolean;
  modelPolicy: boolean;
  executionGraph: boolean;
  gitops: boolean;
  gitopsTurnAction: "commit" | "snapshot" | "status-only";
  gitopsTurnPush: boolean;
  auditEnvelope: boolean;
  planningFlow: boolean;
}

function envForcesLegacyFallback(): boolean {
  const raw = process.env.SF_UOK_FORCE_LEGACY ?? process.env.SF_UOK_LEGACY_FALLBACK;
  if (!raw) return false;
  const normalized = raw.trim().toLowerCase();
  return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
}

export function resolveUokFlags(prefs: SFPreferences | undefined): UokFlags {
  const uok = prefs?.uok;
  const legacyFallback = uok?.legacy_fallback?.enabled === true || envForcesLegacyFallback();
  const enabledByPreference = uok?.enabled ?? true;
  return {
    enabled: enabledByPreference && !legacyFallback,
    legacyFallback,
    gates: uok?.gates?.enabled === true,
    modelPolicy: uok?.model_policy?.enabled === true,
    executionGraph: uok?.execution_graph?.enabled === true,
    gitops: uok?.gitops?.enabled === true,
    gitopsTurnAction: uok?.gitops?.turn_action ?? "status-only",
    gitopsTurnPush: uok?.gitops?.turn_push === true,
    auditEnvelope: uok?.audit_envelope?.enabled === true || uok?.audit_unified?.enabled === true,
    planningFlow: uok?.planning_flow?.enabled === true || uok?.plan_v2?.enabled === true,
  };
}

export function loadUokFlags(): UokFlags {
  const prefs = loadEffectiveSFPreferences()?.preferences;
  return resolveUokFlags(prefs);
}
