import { NextResponse } from "next/server";
import { sql } from "@/db";

/**
 * GET /api/healthz — public, unauthenticated liveness probe.
 *
 * For external monitors (Uptime Kuma) and the Docker / Dokploy healthcheck.
 * Returns NO sensitive detail — only whether the app can reach the database.
 *   200  { status: "ok",   db: true  }
 *   503  { status: "down", db: false }
 */
export const dynamic = "force-dynamic";

export async function GET() {
  try {
    await sql`SELECT 1`;
    return NextResponse.json(
      { status: "ok", db: true, ts: new Date().toISOString() },
      { headers: { "Cache-Control": "no-store" } },
    );
  } catch {
    return NextResponse.json(
      { status: "down", db: false, ts: new Date().toISOString() },
      { status: 503, headers: { "Cache-Control": "no-store" } },
    );
  }
}
