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

// WBS 템플릿 마스터 CUSTOMER_PRODUCT 수정 (project.saveWBSTemplateMasterInfo 대응)
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 { objId, customer_product } = body;
  if (!objId) return NextResponse.json({ success: false, message: "objId 필요" }, { status: 400 });

  try {
    await execute(
      `UPDATE pms_wbs_template SET customer_product = $1 WHERE objid = $2`,
      [customer_product ?? "", objId]
    );
    return NextResponse.json({ success: true, message: "저장되었습니다." });
  } catch (error) {
    console.error("WBS template master save:", error);
    return NextResponse.json({ success: false, message: "저장 중 오류가 발생했습니다." }, { status: 500 });
  }
}
