import { prisma } from '@/lib/db';
import { apiUser } from '@/lib/apiAuth';
import { STATUS_TR } from '@/lib/orders';
import { appUrl } from '@/lib/http';
export const dynamic = 'force-dynamic';

/** B2B sipariş durumu. Teslim edildiyse izleme bağlantısı (varsa) döner. */
export async function GET(req: Request, { params }: { params: Promise<{ id: string }> }) {
  const u = await apiUser(req);
  if (!u) return Response.json({ error: 'unauthorized' }, { status: 401 });
  const { id } = await params;
  const o = await prisma.order.findFirst({ where: { id, userId: u.id } });
  if (!o) return Response.json({ error: 'not_found' }, { status: 404 });
  return Response.json({
    id: o.id, code: o.code, status: o.status, statusText: STATUS_TR[o.status], progress: o.progress,
    deliveredAt: o.deliveredAt, durationSec: o.durationSec, error: o.error,
    watchUrl: o.shareToken ? `${appUrl(req)}/izle/${o.shareToken}` : null,
  });
}
