// 푸시 공지 단건 조회 — 로그인한 모든 사용자 가능 (푸시 탭하면 열리는 페이지가 호출).
import { NextRequest, NextResponse } from "next/server";
import { queryOne } from "@/lib/db";
import { requireMomoUser } from "@/lib/momo-guard";
import { ensureNoticesTable } from "@/lib/notices";

export async function GET(_req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
  const r = await requireMomoUser();
  if (r instanceof NextResponse) return r;
  await ensureNoticesTable();
  const { id } = await ctx.params;
  const row = await queryOne(
    `SELECT objid AS "OBJID", title AS "TITLE", body AS "BODY",
            image_url AS "IMAGE_URL", url AS "URL",
            TO_CHAR(regdate, 'YYYY-MM-DD HH24:MI') AS "REGDATE"
       FROM momo_notices
      WHERE objid = $1 AND COALESCE(is_del,'N') != 'Y'`,
    [id]
  );
  if (!row) return NextResponse.json({ success: false, message: "공지를 찾을 수 없습니다." }, { status: 404 });
  return NextResponse.json({ success: true, notice: row });
}
