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

export async function GET() {
  const user = await getSession();
  if (!user) {
    return NextResponse.json({ success: false }, { status: 401 });
  }
  // 특수 권한 (발주 한도 무시 / 숨김 품목 보기) 도 같이 내려줌
  let perms = { unlimitedQty: false, viewHidden: false };
  try {
    const row = await queryOne<{ U: string; V: string }>(
      `SELECT COALESCE(unlimited_qty, 'N') AS "U", COALESCE(view_hidden, 'N') AS "V"
       FROM user_info WHERE user_id = $1`,
      [user.userId]
    );
    if (row) perms = { unlimitedQty: row.U === "Y", viewHidden: row.V === "Y" };
  } catch { /* ignore */ }
  return NextResponse.json({ success: true, user: { ...user, ...perms } });
}
