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

// common/getCodeList.do 대응 - 공통코드 조회
// CommonService.bizMakeOptionList → common.getCodeselect 대응
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 { codeId } = body;

  if (!codeId) {
    return NextResponse.json({ success: false, message: "codeId required" }, { status: 400 });
  }

  const rows = await queryRows(
    `SELECT code_id AS "CODE_ID", code_name AS "CODE_NAME", objid::text AS "OBJID"
       FROM comm_code
      WHERE parent_code_id = $1 AND COALESCE(status, 'active') = 'active'
      ORDER BY objid`,
    [codeId],
  );

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