import crypto from 'node:crypto';
import { prisma } from '@/lib/db';
import { currentUser } from '@/lib/auth';
export const dynamic = 'force-dynamic';
/** Teslim edilmiş video için herkese açık izleme bağlantısını açar/kapatır. */
export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) {
  const u = await currentUser(); if (!u) return Response.json({ error: 'Giriş gerekli' }, { status: 401 });
  const { id } = await params;
  const o = await prisma.order.findUnique({ where: { id } });
  if (!o || o.userId !== u.id) return Response.json({ error: 'Bulunamadı' }, { status: 404 });
  if (o.status !== 'DELIVERED') return Response.json({ error: 'Video henüz hazır değil' }, { status: 400 });
  const on = (await req.json().catch(() => ({}))).on !== false;
  const token = on ? (o.shareToken || crypto.randomBytes(9).toString('base64url')) : null;
  await prisma.order.update({ where: { id }, data: { shareToken: token } });
  return Response.json({ token });
}
