import { createTask, listTasks } from '@/lib/delegation.js';

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

export async function GET(req) {
  const { searchParams } = new URL(req.url);
  return Response.json({
    tasks: listTasks({
      limit: Number(searchParams.get('limit') || 100),
      assignee_agent_id: searchParams.get('assignee_agent_id') || undefined,
      status: searchParams.get('status') || undefined,
      priority: searchParams.get('priority') || undefined,
      parent_task_id: searchParams.has('parent_task_id') ? (searchParams.get('parent_task_id') || null) : undefined,
    }),
  });
}

export async function POST(req) {
  const body = await req.json().catch(() => ({}));
  return Response.json({ task: createTask(body) }, { status: 201 });
}
