// 특정 수신자 그룹의 멤버 user_id 목록 + 사용자 정보
import { NextRequest, NextResponse } from "next/server";
import { queryRows } from "@/lib/db";
import { requireMomoAdmin } from "@/lib/momo-guard";
import { ensureNoticesTable } from "@/lib/notices";

export async function POST(req: NextRequest) {
  const g = await requireMomoAdmin();
  if (g instanceof NextResponse) return g;
  await ensureNoticesTable();
  const { groupObjid } = await req.json().catch(() => ({}));
  if (!groupObjid) return NextResponse.json({ success: false, message: "groupObjid 필수" }, { status: 400 });
  const rows = await queryRows(
    `SELECT M.user_id AS "USER_ID",
            COALESCE(U.user_name,'') AS "USER_NAME",
            COALESCE(U.user_type,'') AS "USER_TYPE"
       FROM momo_recipient_group_members M
       LEFT JOIN user_info U ON U.user_id = M.user_id
      WHERE M.group_objid = $1
      ORDER BY U.user_name NULLS LAST, M.user_id`,
    [groupObjid]
  );
  return NextResponse.json({ RESULTLIST: rows, TOTAL_CNT: rows.length });
}
