import { attachArtifactToTask, getTask, listTaskAttachments } 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({ attachments: listTaskAttachments(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(() => ({}));
  const artifactId = String(body.artifact_id || '').trim();
  if (!artifactId) return new Response('artifact_id required', { status: 400 });
  return Response.json(attachArtifactToTask(artifactId, id));
}
