import { addChecklistItem, getTask, listTaskChecklist } from '@/lib/delegation.js';

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

export async function GET(_req, { params }) {
  const { id } = await params;
  if (!getTask(id)) return new Response('Task not found', { status: 404 });
  return Response.json({ items: listTaskChecklist(id) });
}

export async function POST(req, { params }) {
  const { id } = await params;
  if (!getTask(id)) return new Response('Task not found', { status: 404 });
  const body = await req.json().catch(() => ({}));
  try {
    const item = addChecklistItem(id, { label: body.label, position: body.position });
    return Response.json({ item }, { status: 201 });
  } catch (err) {
    return new Response(String(err?.message || err), { status: 400 });
  }
}
