// GET  /api/hail/engagements/[id]/pilot  — pilot status (live agents + report)
// POST /api/hail/engagements/[id]/pilot  — (re)render the pilot test report
//
// Practitioner-gated. Pilot agents go live only once the pilot-launch gate is
// approved AND the setup package was applied (provisioned agents).

import { getEngagement } from '../../../../../../lib/hail/engagements.js';
import { getPilotStatus, renderPilotTestReport } from '../../../../../../lib/hail/pilot.js';
import { requirePractitioner, errorResponse } from '../../../guard.js';

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

export async function GET(req, { params }) {
  const { id } = await params;
  try {
    await requirePractitioner(req);
    if (!getEngagement(id)) return Response.json({ error: 'engagement_not_found' }, { status: 404 });
    return Response.json({ pilot: getPilotStatus(id) });
  } catch (err) {
    return errorResponse(err);
  }
}

export async function POST(req, { params }) {
  const { id } = await params;
  try {
    await requirePractitioner(req);
    if (!getEngagement(id)) return Response.json({ error: 'engagement_not_found' }, { status: 404 });
    const art = renderPilotTestReport(id);
    return Response.json({ ok: true, artifact_id: art.artifact_id }, { status: 201 });
  } catch (err) {
    return errorResponse(err);
  }
}
