import packageJson from '@/package.json'
import { prisma } from '@/lib/prisma'

export async function GET(request: Request) {
  const url = new URL(request.url)
  const checkDb = url.searchParams.get('db') === '1'

  const body: Record<string, unknown> = {
    status: 'ok',
    version: packageJson.version,
    time: new Date().toISOString(),
  }

  if (checkDb) {
    try {
      await prisma.$queryRaw`SELECT 1`
      body.database = 'ok'
    } catch {
      body.database = 'down'
    }
  }

  return Response.json(body)
}
