import { NextResponse } from "next/server";
import { getSessionContext } from "@/lib/auth";
import { prisma } from "@/lib/prisma";

async function getOrgId(userId: string) {
}

export async function POST(_req: Request, { params }: { params: Promise<{ id: string }> }) {
  const ctx = await getSessionContext();
    if (!ctx) return NextResponse.json({ success: false, error: "Non authentifié ou organisation introuvable" }, { status: 401 });
    const { userId, orgId } = ctx;

  const { id } = await params;
  const quote = await prisma.quote.findFirst({ where: { id, organizationId: orgId } });
  if (!quote) return NextResponse.json({ success: false, error: "Devis introuvable" }, { status: 404 });
  if (!["SENT", "DRAFT"].includes(quote.status))
    return NextResponse.json({ success: false, error: "Ce devis ne peut pas être accepté" }, { status: 400 });

  const updated = await prisma.quote.update({
    where: { id },
    data: { status: "ACCEPTED", acceptedAt: new Date() },
  });

  return NextResponse.json({ success: true, data: updated });
}
