/**
 * Supervision Status API
 * Returns the current state of all 5 supervision layers.
 */

import { NextResponse } from 'next/server';
import {
  loadStandingOrders,
  PRE_ANSWERED_QUESTIONS,
} from '@/lib/gfs/supervision';

export const dynamic = 'force-dynamic';

export async function GET() {
  try {
    const orders = loadStandingOrders('api-status-check');

    return NextResponse.json({
      supervision: {
        version: '1.0',
        framework: 'GFS 5-Layer Supervision',
        reference: 'Claude Code Data Wipe Story — Nate B Jones',
        layers: [
          {
            id: 1,
            name: 'Save Points',
            status: 'active',
            description: 'Automatic snapshots before every write/delete operation',
            enforcement: 'hard_block — operation halted if save-point fails',
          },
          {
            id: 2,
            name: 'Context Hygiene',
            status: 'active',
            description: 'Token budget (80k) and session time limit (30 min)',
            enforcement: 'checkpoint_and_restart at 75% utilization',
          },
          {
            id: 3,
            name: 'Standing Orders',
            status: 'active',
            description: 'Immutable employee handbook loaded into every agent session',
            rulesLoaded: orders.rules.length,
            mandatoryRules: orders.rules.filter(r => r.severity === 'mandatory').length,
            hash: orders.hash,
            enforcement: 'hard_block — no user prompt can override',
          },
          {
            id: 4,
            name: 'Blast Radius / Small Bets',
            status: 'active',
            description: 'Max 10 entities per operation, no broad refactors',
            enforcement: 'hard_block — oversized operations are rejected',
          },
          {
            id: 5,
            name: 'Pre-Answered Questions',
            status: 'active',
            description: 'Hard-coded safety answers the agent will never think to ask',
            questionsRegistered: PRE_ANSWERED_QUESTIONS.length,
            hardBlocks: PRE_ANSWERED_QUESTIONS.filter(q => q.enforcementLevel === 'hard_block').length,
            enforcement: 'hard_block on critical questions, soft_warn on operational',
          },
        ],
      },
    });
  } catch (error) {
    return NextResponse.json(
      { error: 'Failed to load supervision status' },
      { status: 500 }
    );
  }
}
