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

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 required" });

  const info = await queryOne(
    `SELECT P.objid::text AS "OBJID",
       P.part_no AS "PART_NO", P.part_name AS "PART_NAME",
       P.spec AS "SPEC", P.material AS "MATERIAL", P.weight AS "WEIGHT",
       P.unit AS "UNIT", P.qty AS "QTY", P.part_type AS "PART_TYPE",
       P.maker AS "MAKER", P.post_processing AS "POST_PROCESSING",
       P.remark AS "REMARK", P.sourcing_code AS "SOURCING_CODE",
       P.revision AS "REVISION", P.status AS "STATUS", P.is_last AS "IS_LAST",
       P.thickness AS "THICKNESS", P.width AS "WIDTH", P.height AS "HEIGHT",
       P.out_diameter AS "OUT_DIAMETER", P.in_diameter AS "IN_DIAMETER", P.length AS "LENGTH",
       P.major_category AS "MAJOR_CATEGORY", P.sub_category AS "SUB_CATEGORY",
       P.product_mgmt_objid AS "PRODUCT_MGMT_OBJID", P.upg_no AS "UPG_NO",
       P.change_type AS "CHANGE_TYPE", P.change_option AS "CHANGE_OPTION",
       P.eo_no AS "EO_NO", P.eo_date AS "EO_DATE",
       P.design_apply_point AS "DESIGN_APPLY_POINT",
       P.design_date AS "DESIGN_DATE",
       P.management_flag AS "MANAGEMENT_FLAG",
       P.supply_code AS "SUPPLY_CODE",
       P.sub_material AS "SUB_MATERIAL",
       P.writer AS "WRITER"
     FROM part_mng P WHERE P.objid = $1`, [objId]
  );
  if (!info) return NextResponse.json({ success: false, message: "데이터를 찾을 수 없습니다." });
  return NextResponse.json({ success: true, data: info });
}
