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

// WBS 템플릿 Task 삭제 (project.deleteWBSTemplateTask 대응)
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 (!Array.isArray(objIds) || objIds.length === 0) {
    return NextResponse.json({ success: false, message: "선택된 대상이 없습니다." });
  }

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