import { prisma } from '@/lib/db';
import { agentGuard } from '@/lib/agentRoute';
export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) {
  const bad = await agentGuard(req); if (bad) return bad;
  const { id } = await params;
  const body = await req.json().catch(() => ({}));
  const agent = String(body.agent || 'external-agent').slice(0, 60);
  const r = await prisma.order.updateMany({ where: { id, claimedBy: null, status: { in: ['PAID', 'QUEUED'] } }, data: { claimedBy: agent, status: 'RENDERING', progress: 1, progressMsg: 'Harici üretici sahiplendi' } });
  if (!r.count) return Response.json({ error: 'Sipariş alınamaz (sahiplenilmiş veya uygun değil)' }, { status: 409 });
  await prisma.job.updateMany({ where: { orderId: id, status: 'queued' }, data: { status: 'external', lockedBy: agent, lockedAt: new Date() } });
  return Response.json({ ok: true });
}
