import { NextRequest, NextResponse } from 'next/server';
import { checkDeleteAllowed, GUARDRAIL_RULES, VOID_REASON_CODES } from '@/lib/gfs/guardrails';
import type { GuardrailEntity } from '@/lib/gfs/guardrails';

export const dynamic = 'force-dynamic';

/**
 * GET /api/gfs/guardrails — Get guardrail rules + reason codes
 * GET /api/gfs/guardrails?check=delete&entity=customer&id=xxx — Pre-flight delete check
 */
export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url);
  const check = searchParams.get('check');
  const entity = searchParams.get('entity') as GuardrailEntity | null;
  const id = searchParams.get('id');

  // Pre-flight delete check
  if (check === 'delete' && entity && id) {
    const result = await checkDeleteAllowed(entity, id);
    return NextResponse.json(result);
  }

  // Return all rules + reason codes
  return NextResponse.json({
    rules: GUARDRAIL_RULES,
    voidReasonCodes: VOID_REASON_CODES,
    enforcement: 'hard — non-overridable at DB, API, UI, Agent, and The Hearth levels',
  });
}
