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

// WBS 템플릿 마스터 상세 (project.getWBSTemplateMasterInfo 대응)
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });

  const { objId } = await request.json();
  if (!objId) return NextResponse.json({ success: false, message: "objId 필요" }, { status: 400 });

  const row = await queryOne<Record<string, unknown>>(
    `SELECT T.objid::text AS "OBJID",
       T.product_objid AS "PRODUCT_OBJID",
       COALESCE((SELECT code_name FROM comm_code WHERE code_id = T.product_objid LIMIT 1), '') AS "PRODUCT_OBJID_NAME",
       T.title AS "TITLE",
       T.writer AS "WRITER",
       T.customer_product AS "CUSTOMER_PRODUCT"
     FROM pms_wbs_template T
     WHERE T.objid = $1`,
    [objId]
  );

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