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

// 원본: salesMng.getSalesRequestMasterInfo + salesMngNew.getSalesRequestSavedPartList
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 SRM.objid::text AS "OBJID",
       SRM.request_mng_no AS "REQUEST_MNG_NO",
       SRM.request_cd AS "REQUEST_CD",
       SRM.project_no AS "PROJECT_NO",
       (SELECT COALESCE(project_no, contract_no) FROM project_mgmt WHERE objid = SRM.project_no LIMIT 1) AS "PROJECT_NUMBER",
       (SELECT customer_project_name FROM project_mgmt WHERE objid = SRM.project_no LIMIT 1) AS "PROJECT_NAME",
       (SELECT supply_name FROM supply_mng WHERE objid::text = (SELECT customer_objid FROM project_mgmt WHERE objid = SRM.project_no LIMIT 1)::text LIMIT 1) AS "CUSTOMER_NAME",
       (SELECT mechanical_type FROM project_mgmt WHERE objid = SRM.project_no LIMIT 1) AS "MECHANICAL_TYPE",
       (SELECT setup FROM project_mgmt WHERE objid = SRM.project_no LIMIT 1) AS "SETUP",
       SRM.release_date AS "RELEASE_DATE",
       SRM.request_reasons AS "REQUEST_REASONS",
       SRM.request_user_id AS "REQUEST_USER_ID",
       SRM.delivery_request_date AS "DELIVERY_REQUEST_DATE",
       SRM.unit_name AS "UNIT_NAME",
       SRM.status AS "STATUS",
       CASE SRM.status
         WHEN 'create' THEN '등록'
         WHEN 'release' THEN '제출 완료'
         WHEN 'reception' THEN '접수'
         WHEN 'approvalRequest' THEN '결재중'
         WHEN 'approvalComplete' THEN '결재완료'
         WHEN 'reject' THEN '반려'
         ELSE ''
       END AS "STATUS_TITLE",
       SRM.receipt_user_id AS "RECEIPT_USER_ID",
       SRM.receipt_date AS "RECEIPT_DATE",
       SRM.writer AS "WRITER",
       SRM.remark AS "REMARK",
       TO_CHAR(SRM.regdate, 'YYYY-MM-DD') AS "REGDATE_TITLE"
     FROM sales_request_master SRM
     WHERE SRM.objid::text = $1`,
    [objId],
  );
  if (!info) return NextResponse.json({ success: false, message: "데이터를 찾을 수 없습니다." });

  const parts = await queryRows(
    `SELECT SRP.objid::text AS "OBJID",
       SRP.sales_bom_qty_objid AS "SALES_BOM_QTY_OBJID",
       SRP.part_objid AS "PART_OBJID",
       (SELECT part_no FROM part_mng WHERE objid::text = SRP.part_objid LIMIT 1) AS "PART_NO",
       COALESCE(SRP.part_name, (SELECT part_name FROM part_mng WHERE objid::text = SRP.part_objid LIMIT 1)) AS "PART_NAME",
       SRP.qty AS "QTY",
       SRP.org_qty AS "ORG_QTY",
       SRP.partner_objid AS "PARTNER_OBJID",
       SRP.delivery_request_date AS "DELIVERY_REQUEST_DATE",
       SRP.remark AS "REMARK"
     FROM sales_request_part SRP
     WHERE SRP.sales_request_master_objid = $1
     ORDER BY SRP.regdate`,
    [objId],
  );

  return NextResponse.json({ success: true, data: info, PARTS: parts });
}
