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

// 입출고 History 팝업 (getInventoryRequestHistoryList 대응)
// 원본: inventoryMng.xml line 2142
// 입고(inventory_mgmt_in, move_date 없음) + 출고(inventory_mgmt_out) + 이동(inventory_mgmt_in, move_date 있음)
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 = String(body.objId || body.OBJID || "");
  if (!objId) return NextResponse.json({ RESULTLIST: [] });

  const rows = await queryRows(
    `SELECT
        (SELECT MAX(parent_objid) FROM arrival_plan O
           WHERE O.part_objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "OBJID",
        (SELECT part_no FROM part_mng O
           WHERE O.objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "PART_NO",
        (SELECT part_name FROM part_mng O
           WHERE O.objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "PART_NAME",
        '입고' AS "GUBUN",
        T.receipt_qty AS "RECEIPT_QTY",
        (SELECT code_name FROM comm_code WHERE code_id = T.location) AS "LOCATION_NAME",
        (SELECT code_name FROM comm_code WHERE code_id = T.sub_location) AS "SUB_LOCATION_NAME",
        (SELECT project_no FROM project_mgmt PM WHERE PM.objid = T.contract_mgmt_objid) AS "PROJECT_NO"
      FROM inventory_mgmt_in T
      WHERE T.parent_objid = $1
        AND (T.move_date IS NULL OR T.move_date = '')
    UNION ALL
      SELECT
        T2.objid AS "OBJID",
        (SELECT part_no FROM part_mng O
           WHERE O.objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "PART_NO",
        (SELECT part_name FROM part_mng O
           WHERE O.objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "PART_NAME",
        '출고' AS "GUBUN",
        T.out_qty AS "RECEIPT_QTY",
        (SELECT code_name FROM comm_code WHERE code_id = T1.location) AS "LOCATION_NAME",
        (SELECT code_name FROM comm_code WHERE code_id = T1.sub_location) AS "SUB_LOCATION_NAME",
        (SELECT project_no FROM project_mgmt PM WHERE PM.objid = T.contract_mgmt_objid) AS "PROJECT_NO"
      FROM inventory_mgmt_out T
      LEFT JOIN inventory_mgmt T1 ON T.parent_objid = T1.objid
      LEFT JOIN inventory_mgmt_out_master T2 ON T.inventory_request_master_objid = T2.objid
      WHERE T.parent_objid = $1
    UNION ALL
      SELECT
        (SELECT MAX(parent_objid) FROM arrival_plan O
           WHERE O.part_objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "OBJID",
        (SELECT part_no FROM part_mng O
           WHERE O.objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "PART_NO",
        (SELECT part_name FROM part_mng O
           WHERE O.objid = (SELECT part_objid FROM inventory_mgmt O1 WHERE O1.objid = T.parent_objid)) AS "PART_NAME",
        '이동' AS "GUBUN",
        T.receipt_qty AS "RECEIPT_QTY",
        (SELECT code_name FROM comm_code WHERE code_id = T.location) AS "LOCATION_NAME",
        (SELECT code_name FROM comm_code WHERE code_id = T.sub_location) AS "SUB_LOCATION_NAME",
        (SELECT project_no FROM project_mgmt PM WHERE PM.objid = T.contract_mgmt_objid) AS "PROJECT_NO"
      FROM inventory_mgmt_in T
      WHERE T.parent_objid = $1
        AND T.move_date IS NOT NULL
        AND T.move_date != ''
`,
    [objId],
  );

  return NextResponse.json({ RESULTLIST: rows });
}
