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

async function getOrgId(userId: string) {
}

export async function POST(
  _req: NextRequest,
  { 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 existing = await prisma.interventionOrder.findFirst({ where: { id, organizationId: orgId } });
  if (!existing) return NextResponse.json({ success: false, error: "Bon introuvable" }, { status: 404 });

  if (["DONE", "CANCELED"].includes(existing.status)) {
    return NextResponse.json({ success: false, error: "Impossible d'annuler depuis ce statut" }, { status: 400 });
  }

  const updated = await prisma.interventionOrder.update({
    where: { id },
    data: { status: "CANCELED", canceledAt: new Date() },
  });

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