// app/hail/[id]/governance/page.js — governance matrix editor.
//
// Server shell loads the engagement + current rules and renders the client
// editor. Each row is re-validated server-side on save (PUT); irreversible_external
// rules must require approval and name a reviewer.

import Link from 'next/link';
import { notFound } from 'next/navigation';
import { AdminShell } from '../../../components/admin/admin-page.jsx';
import { getEngagement } from '../../../../lib/hail/engagements.js';
import { listGovernanceRules } from '../../../../lib/hail/blueprint.js';
import { requirePractitionerPage } from '../../hail-guard.js';
import GovernanceEditor from './governance-editor.jsx';

export const dynamic = 'force-dynamic';
export const runtime = 'nodejs';

export default async function GovernancePage({ params }) {
  await requirePractitionerPage();
  const { id } = await params;
  const engagement = getEngagement(id);
  if (!engagement) notFound();
  const rules = listGovernanceRules(id);

  return (
    <AdminShell
      kicker="madarX OS · HAIL"
      title={`Governance matrix — ${engagement.principal_name}`}
      description="What each agent function may decide, draft, recommend, execute, or must escalate — and what requires human approval. Mirrors the madarX OS trust-mode / approval / audit model."
      hideProjectSwitcher
      actions={<Link href={`/hail/${id}`} className="hail-btn">← Cockpit</Link>}
    >
      <GovernanceEditor engagementId={id} initialRules={rules} />
    </AdminShell>
  );
}
