// 모든 사용자 목록 — 수신자 그룹 멤버 추가/제거 시 picker 용.
//   현재 푸시 구독 여부도 표시해서 admin 이 한눈에 알 수 있게 한다.
import { NextResponse } from "next/server";
import { queryRows } from "@/lib/db";
import { requireMomoAdmin } from "@/lib/momo-guard";

export async function POST() {
  const g = await requireMomoAdmin();
  if (g instanceof NextResponse) return g;
  const rows = await queryRows(
    `SELECT U.user_id              AS "USER_ID",
            COALESCE(U.user_name,'') AS "USER_NAME",
            COALESCE(U.user_type,'') AS "USER_TYPE",
            COALESCE(U.user_type,'')='A' AS "IS_ADMIN",
            COALESCE(U.dept_name,'') AS "DEPT_NAME",
            EXISTS (SELECT 1 FROM momo_push_subscriptions S WHERE S.user_id = U.user_id) AS "SUBSCRIBED"
       FROM user_info U
      WHERE COALESCE(U.status,'active') = 'active'
      ORDER BY U.user_type DESC NULLS LAST, U.user_name NULLS LAST, U.user_id`
  );
  return NextResponse.json({ RESULTLIST: rows, TOTAL_CNT: rows.length });
}
