import { NextRequest, NextResponse } from "next/server";
import { execute } from "@/lib/db";
import { getSession } from "@/lib/session";

// 발주서 삭제 (purchaseOrder.deletePurchaseOrderMaster 대응)
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });

  const { objIds } = await request.json();
  if (!objIds?.length) return NextResponse.json({ success: false, message: "삭제할 항목을 선택하세요." });

  try {
    const ph = objIds.map((_: string, i: number) => `$${i + 1}`).join(",");
    await execute(`DELETE FROM purchase_order_part WHERE purchase_order_master_objid IN (${ph})`, objIds);
    await execute(`DELETE FROM purchase_order_master WHERE objid IN (${ph})`, objIds);
    return NextResponse.json({ success: true, message: `${objIds.length}건이 삭제되었습니다.` });
  } catch (error) {
    console.error("Purchase order delete:", error);
    return NextResponse.json({ success: false, message: "삭제 중 오류가 발생했습니다." }, { status: 500 });
  }
}
