import { json } from '@sveltejs/kit';
import { getPool } from '$lib/server/db';

export async function GET() {
  const pool = getPool();
  let database: 'disabled' | 'up' | 'down' = 'disabled';

  if (pool) {
    try {
      await pool.query('select 1');
      database = 'up';
    } catch (error) {
      console.error('Health check database query failed.', error);
      database = 'down';
    }
  }

  return json(
    {
      status: database === 'down' ? 'degraded' : 'ok',
      database,
      timestamp: new Date().toISOString()
    },
    {
      status: database === 'down' ? 503 : 200
    }
  );
}
