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

// 고객 삭제 (원본: contractMgmt.xml deletesupplyMngInfo — 실제 DELETE)
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });

  const body = await request.json();
  const objIds: string[] = Array.isArray(body.objIds) ? body.objIds : body.objId ? [body.objId] : [];

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

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