// lib/hail/constants.js — canonical enums for the HAIL operating-model layer.
//
// HAIL (Human-to-Agent Intelligence Layer) is the operating-model configuration
// layer that interviews a principal, produces a set of approved operating-model
// artifacts, and uses them to configure madarX OS (agents, tools, workflows,
// memory, schedules, secrets, approvals, audit, artifact ACLs).
//
// This module is PURE — no DB, no fs, no network. It is the single source of
// truth for the vocabularies the HAIL validators, schema, API, and UI share.
// Keep these aligned with the existing runtime safety model: the governance
// risk tiers MUST mirror lib/process/spec-validator.js::VALID_RISK_TIERS plus
// the `blocked` tier from ai-docs/agent-security-model.md.

// Sensitivity classification applied to every captured answer AT CAPTURE.
// `refused` means: do not persist the value — store only the marker that the
// principal declined to provide it. Ordered least→most restrictive.
export const SENSITIVITY_CLASSES = Object.freeze([
  'public', // no restriction
  'internal', // workspace-internal, non-sensitive
  'confidential', // need-to-know within the engagement
  'restricted', // highest — local-only, never leaves principal infrastructure
  'refused', // principal declined; value not stored
]);
export const SENSITIVITY_SET = new Set(SENSITIVITY_CLASSES);

// The four HAIL engagement lifecycle stages (Diagnostic → Blueprint → Pilot →
// Managed Layer). The first three run inside a single process-engine case; the
// Managed Layer is an ongoing state entered when the case completes, with
// quarterly reviews modelled as separate cases.
export const LIFECYCLE_STAGES = Object.freeze([
  'diagnostic',
  'blueprint',
  'pilot',
  'managed_layer',
]);
export const LIFECYCLE_STAGE_SET = new Set(LIFECYCLE_STAGES);

// Governance risk tiers — MUST match the runtime tool/approval model exactly.
// `blocked` → refuse | `irreversible_external` → always gate (even trust=auto)
// | `high`/`medium` → gate in approval_required | `low` → execute.
export const GOVERNANCE_RISK_TIERS = Object.freeze([
  'low',
  'medium',
  'high',
  'irreversible_external',
  'blocked',
]);
export const GOVERNANCE_RISK_SET = new Set(GOVERNANCE_RISK_TIERS);

// What an agent function may do with a given action, per the governance matrix.
export const AGENT_AUTHORITIES = Object.freeze([
  'decide', // act without per-instance human review (low-risk only)
  'draft', // prepare output for human review; never sends
  'recommend', // surface an option; human chooses
  'execute', // perform an external action (always gated above `low`)
  'escalate', // route to a named human reviewer
]);
export const AGENT_AUTHORITY_SET = new Set(AGENT_AUTHORITIES);

// Durable HAIL artifact kinds. Each renders as HTML/Markdown via createArtifact
// and may carry a structured JSON payload reference in the HAIL tables.
export const HAIL_ARTIFACT_KINDS = Object.freeze([
  'interview_transcript',
  'diagnostic_report',
  'operating_profile',
  'role_map',
  'friction_map',
  'governance_matrix',
  'agent_roster',
  'setup_plan',
  'pilot_test_report',
  'quarterly_review',
]);
export const HAIL_ARTIFACT_KIND_SET = new Set(HAIL_ARTIFACT_KINDS);

// Review gates — no HAIL-generated configuration becomes active without one.
export const REVIEW_GATE_TYPES = Object.freeze([
  'diagnostic_signoff',
  'blueprint_signoff',
  'setup_package_approval',
  'pilot_launch',
  'managed_layer_change',
]);
export const REVIEW_GATE_SET = new Set(REVIEW_GATE_TYPES);

// Human reviewer roles that may be assigned to a gate or a governance rule.
export const REVIEWER_ROLES = Object.freeze([
  'practitioner',
  'architect',
  'security_reviewer',
  'principal',
  'counsel',
]);
export const REVIEWER_ROLE_SET = new Set(REVIEWER_ROLES);

// Setup-package item types. Each maps to exactly one madarX OS runtime surface
// the HAIL setup applier knows how to provision (always dry-run first, always
// approval-gated, always audited before any runtime mutation).
export const SETUP_ITEM_TYPES = Object.freeze([
  'agent', // lib/runtime.js::createAgent
  'tool', // marketplace tool install / enable
  'allowlist_entry', // lib/tool-allowlists.js::addEntry
  'secret', // lib/secrets.js::setSecret (value never in the package)
  'schedule', // lib/runtime.js::upsertSchedule
  'workflow', // lib/process/definitions.js::upsertDefinition
  'memory_scope', // agent_memory scope declaration
  'artifact_acl', // default ACL scope for a class of artifacts
  'approval_policy', // agent_tool_permissions / trust posture
]);
export const SETUP_ITEM_TYPE_SET = new Set(SETUP_ITEM_TYPES);

// Confidence levels the diagnostic agent attaches so practitioners can see what
// is observed vs inferred vs needs confirmation.
export const CONFIDENCE_LEVELS = Object.freeze(['observed', 'inferred', 'needs_confirmation']);
export const CONFIDENCE_SET = new Set(CONFIDENCE_LEVELS);

// The HAIL Configuration Interview sections (PRD Feature 1), in order. The
// interview is sectioned + resumable; `section_cursor` on hail_interviews holds
// the last section the principal/practitioner was on.
export const INTERVIEW_SECTIONS = Object.freeze([
  { slug: 'principal_identity', label: 'Principal identity & context' },
  { slug: 'role_capacity_inventory', label: 'Role / capacity inventory' },
  { slug: 'decision_delegation_style', label: 'Decision & delegation style' },
  { slug: 'reputation_constraints', label: 'Reputation constraints' },
  { slug: 'confidentiality_boundaries', label: 'Confidentiality boundaries' },
  { slug: 'current_systems_tools', label: 'Current systems, tools & communications' },
  { slug: 'bottlenecks_friction', label: 'Work bottlenecks & friction points' },
  { slug: 'approval_preferences', label: 'Approval preferences' },
  { slug: 'counterparties_reviewers', label: 'Human counterparties & reviewer roles' },
  { slug: 'regulated_obligations', label: 'Regulated / counsel / audit obligations' },
  { slug: 'desired_outcomes', label: 'Desired outcomes (time / judgment / reputation)' },
]);
export const INTERVIEW_SECTION_SET = new Set(INTERVIEW_SECTIONS.map((s) => s.slug));

export const HAIL_ENGAGEMENT_WORKFLOW_SLUG = 'hail-engagement-lifecycle';

// Maps a lifecycle workflow stage slug (a human_approval gate) onto its HAIL
// review-gate type + the reviewer role responsible. Used to mirror engine
// approval decisions into the hail_review_gates ledger. The slugs MUST match
// lib/process/seeds/specs/hail-engagement-lifecycle.json.
export const LIFECYCLE_GATE_STAGES = Object.freeze({
  'diagnostic-signoff': { gate_type: 'diagnostic_signoff', reviewer_role: 'practitioner' },
  'blueprint-signoff': { gate_type: 'blueprint_signoff', reviewer_role: 'architect' },
  'setup-package-approval': { gate_type: 'setup_package_approval', reviewer_role: 'principal' },
  'pilot-activation': { gate_type: 'pilot_launch', reviewer_role: 'principal' },
  'managed-layer-activation': { gate_type: 'managed_layer_change', reviewer_role: 'practitioner' },
});

// Gate stages of the ongoing-Managed-Layer quarterly review workflow. The
// reconfig-approval gate is the same `setup_package_approval` type so the delta
// setup package routes through the SAME approval → apply path as the lifecycle.
export const QUARTERLY_GATE_STAGES = Object.freeze({
  'review-signoff': { gate_type: 'managed_layer_change', reviewer_role: 'practitioner' },
  'reconfig-approval': { gate_type: 'setup_package_approval', reviewer_role: 'principal' },
});

// Engines that keep data fully on-device. Restricted (local-only) interview
// content is masked before being handed to any engine NOT in this set — matches
// the existing convention in lib/disk.js / lib/agent-memory.js (ollama-only).
export const LOCAL_ENGINES = new Set(['ollama', 'local_whisper']);
export function isLocalEngine(engine) {
  return LOCAL_ENGINES.has(String(engine || ''));
}
export const HAIL_QUARTERLY_REVIEW_WORKFLOW_SLUG = 'hail-quarterly-review';

// Resolve the gate-stage map for a HAIL workflow definition slug. Used by the
// review-gate listener to handle both the engagement lifecycle and quarterly
// reviews without branching per definition.
export const HAIL_WORKFLOW_GATE_STAGES = Object.freeze({
  [HAIL_ENGAGEMENT_WORKFLOW_SLUG]: LIFECYCLE_GATE_STAGES,
  [HAIL_QUARTERLY_REVIEW_WORKFLOW_SLUG]: QUARTERLY_GATE_STAGES,
});

// Hard caps (defensive, mirror the workflow validator's posture).
export const MAX_SETUP_PACKAGE_BYTES = 256 * 1024;
export const MAX_SETUP_ITEMS = 200;
