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.product_category AS "PRODUCT_CATEGORY",
       P.product_type AS "PRODUCT_TYPE", P.product_name AS "PRODUCT_NAME",
       P.product_code AS "PRODUCT_CODE", P.product_name_code AS "PRODUCT_NAME_CODE",
       P.product_grade AS "PRODUCT_GRADE", P.product_ton AS "PRODUCT_TON",
       P.product_boom AS "PRODUCT_BOOM", P.product_vehicle AS "PRODUCT_VEHICLE",
       P.production_flag AS "PRODUCTION_FLAG", P.note AS "NOTE",
       TO_CHAR(P.regdate, 'YYYY-MM-DD') AS "REGDATE"
     FROM product_mgmt P WHERE P.objid = $1`, [objId]
  );
  if (!info) return NextResponse.json({ success: false, message: "데이터를 찾을 수 없습니다." });
  return NextResponse.json({ success: true, data: info });
}
