import { NextRequest, NextResponse } from "next/server";
import { pool } 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 body = await request.json();
  const client = await pool.connect();
  try {
    const result = await client.query(
      `SELECT user_id AS "user_id", user_name AS "user_name", sabun AS "sabun",
        dept_code AS "dept_code", dept_name AS "dept_name", position_name AS "position_name",
        email AS "email", cell_phone AS "cell_phone", user_type AS "user_type",
        tel AS "tel", address AS "address", ceo_name AS "ceo_name", biz_no AS "biz_no",
        status AS "status",
        COALESCE(unlimited_qty, 'N') AS "unlimited_qty",
        COALESCE(view_hidden, 'N') AS "view_hidden",
        default_wh_objid::text AS "default_wh_objid",
        COALESCE(statement_branch, 'HQ') AS "statement_branch",
        TO_CHAR(regdate, 'YYYY-MM-DD') AS "regdate"
       FROM user_info WHERE user_id = $1`,
      [body.userId || ""]
    );
    return NextResponse.json({ success: true, data: result.rows[0] || null });
  } catch (e) {
    console.error("User detail:", e);
    return NextResponse.json({ success: false, message: "조회 중 오류가 발생했습니다." });
  } finally { client.release(); }
}
