/**
 * Supervision Validation API
 * Validates a proposed operation against all 5 supervision layers.
 */

import { NextRequest, NextResponse } from 'next/server';
import { runSupervisionCheck } from '@/lib/gfs/supervision';

export const dynamic = 'force-dynamic';

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();

    const result = runSupervisionCheck({
      operation: {
        type: body.operationType || 'read',
        entityType: body.entityType || 'unknown',
        entityCount: body.entityCount || 1,
        description: body.description || '',
        affectedComponents: body.affectedComponents,
        fileChanges: body.fileChanges,
      },
      session: {
        sessionId: body.sessionId || 'api-validation',
        agentId: body.agentId || 'manual',
        startedAt: new Date(body.sessionStartedAt || Date.now()),
        tokenCount: body.tokenCount || 0,
        taskCount: body.taskCount || 0,
        lastCheckpointAt: null,
        checkpointCount: 0,
      },
      flags: {
        involvesPII: body.involvesPII,
        isProduction: body.isProduction,
        isExternalAPI: body.isExternalAPI,
        riskScore: body.riskScore,
        hasSavePoint: body.hasSavePoint,
        hasHumanApproval: body.hasHumanApproval,
        touchesAuth: body.touchesAuth,
      },
    });

    return NextResponse.json({
      validation: result,
      timestamp: new Date().toISOString(),
    });
  } catch (error) {
    return NextResponse.json(
      { error: 'Validation failed', details: error instanceof Error ? error.message : 'Unknown' },
      { status: 400 }
    );
  }
}
