import { NextResponse } from "next/server";

const PAYLOAD_URL = process.env.PAYLOAD_URL || "https://payload.mk360.de";

// Deliberately checks Payload connectivity, not just "did this route
// handler run" — the site can return 200s from every static/ISR page
// while Payload itself is unreachable (stale cached content masks it for
// a while). Meant for a Kuma HTTP monitor, added to the existing "Content
// & API" group alongside the direct Payload monitors (see ~/dev/README.md).
export async function GET() {
  try {
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), 3000);
    const res = await fetch(`${PAYLOAD_URL}/api/posts?limit=1`, { signal: controller.signal, cache: "no-store" });
    clearTimeout(timeout);
    if (!res.ok) return NextResponse.json({ ok: false, payload: false }, { status: 503 });
    return NextResponse.json({ ok: true });
  } catch {
    return NextResponse.json({ ok: false, payload: false }, { status: 503 });
  }
}
