// GET  /api/hail/engagements/[id]/interview  — list interview sessions (practitioner only)
// POST /api/hail/engagements/[id]/interview  — start a new interview session

import { getEngagement } from '../../../../../../lib/hail/engagements.js';
import { startInterview, listInterviews } from '../../../../../../lib/hail/interview.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({ interviews: listInterviews(id) });
  } catch (err) {
    return errorResponse(err);
  }
}

export async function POST(req, { params }) {
  const { id } = await params;
  let body = {};
  try { body = await req.json(); } catch { /* body optional */ }
  try {
    const session = await requirePractitioner(req);
    if (!getEngagement(id)) return Response.json({ error: 'engagement_not_found' }, { status: 404 });
    const iv = startInterview({
      engagement_id: id,
      title: body?.title || null,
      conducted_by_user_id: session.user_id || null,
    });
    return Response.json({ ok: true, interview: iv }, { status: 201 });
  } catch (err) {
    return errorResponse(err);
  }
}
