import { NextResponse } from "next/server";
import { sql } from "drizzle-orm";
import { getDb } from "@/core/db/client";

/**
 * Readiness probe — verifies app + database connectivity.
 * Must never return secrets or connection strings.
 */
export async function GET() {
  const checks: Record<string, boolean> = {
    app: true,
    database: false,
  };

  try {
    const db = getDb();
    await db.execute(sql`select 1`);
    checks.database = true;
  } catch {
    checks.database = false;
  }

  const ready = Object.values(checks).every(Boolean);

  return NextResponse.json(
    {
      status: ready ? "ready" : "not_ready",
      checks,
    },
    { status: ready ? 200 : 503 },
  );
}
