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

// 프로젝트 선택 시 고객사/셋업지/출고일 자동 조회 (releaseMgmt.getReleaseMgmtList 대응)
export async function POST(request: NextRequest) {
  const user = await getSession();
  if (!user) return NextResponse.json({ success: false }, { status: 401 });

  const { projectObjId } = await request.json();
  if (!projectObjId) return NextResponse.json({ success: false, message: "projectObjId required" });

  const row = await queryOne(
    `SELECT (SELECT supply_name FROM supply_mng WHERE objid::text = CM.customer_objid) AS "CUSTOMER_NAME",
            CM.setup AS "SETUP",
            RM.release_date AS "RELEASE_DATE"
       FROM project_mgmt CM
       LEFT JOIN release_mgmt RM ON CM.objid = RM.parent_objid
      WHERE CM.objid = $1
      LIMIT 1`,
    [String(projectObjId)]
  );

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