import { listCases, startCase } from '../../../lib/process/engine.js';

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

export async function GET(req) {
  const { searchParams } = new URL(req.url);
  const cases = listCases({
    project_id: searchParams.get('project_id') || undefined,
    status: searchParams.get('status') || undefined,
    definition_slug: searchParams.get('definition_slug') || undefined,
    limit: Number(searchParams.get('limit') || 50),
  });
  return Response.json({ cases });
}

export async function POST(req) {
  try {
    const body = await req.json();
    const c = startCase({
      definition_slug: body.definition_slug,
      project_id: body.project_id,
      title: body.title,
      intake_payload: body.intake_payload || null,
      created_by_user_id: body.created_by_user_id || null,
    });
    return Response.json({ case: c }, { status: 201 });
  } catch (err) {
    return Response.json({ error: err?.message || String(err) }, { status: 400 });
  }
}
