import { NextResponse } from 'next/server';
import { prisma } from '@/lib/access';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

/** Readiness endpoint: checks DB reachability without leaking connection details. */
export async function GET() {
  try {
    await prisma.$queryRaw`SELECT 1`;
    const adminConfigured = (process.env.TACHY_ADMIN_KEY?.length ?? 0) >= 32;
    if (!adminConfigured) {
      return NextResponse.json({ status: 'not_ready', reason: 'admin_not_configured' }, { status: 503, headers: { 'Cache-Control': 'no-store' } });
    }
    return NextResponse.json({ status: 'ready' }, { headers: { 'Cache-Control': 'no-store' } });
  } catch {
    return NextResponse.json({ status: 'not_ready', reason: 'database_unavailable' }, { status: 503, headers: { 'Cache-Control': 'no-store' } });
  }
}
