import { NextResponse } from 'next/server'

import { prisma } from '@/lib/db'

export const dynamic = 'force-dynamic'

/**
 * Liveness and readiness in one endpoint.
 *
 * Public by design so a container orchestrator can reach it without credentials, and it
 * deliberately reports only whether the database answers — never a version, a path, or
 * anything else that would help someone map the deployment.
 */
export async function GET() {
  try {
    await prisma.$queryRaw`SELECT 1`
    return NextResponse.json({ ok: true, data: { status: 'healthy', database: 'up' } })
  } catch {
    return NextResponse.json(
      {
        ok: false,
        error: {
          code: 'SERVER_ERROR',
          message: 'Database unavailable.',
          messageAr: 'قاعدة البيانات غير متاحة.',
        },
      },
      { status: 503 },
    )
  }
}
