<?php
declare(strict_types=1);

namespace Infolab\Controllers;

use Infolab\Database;
use Infolab\Request;
use Infolab\Response;

/**
 * GET /api/health
 * Sonde de vie de l'API + ping rapide BDD. Renvoie 503 si la BDD ne répond pas.
 */
final class HealthController
{
    public function index(Request $req, array $params): void
    {
        try {
            Database::fetchValue('SELECT 1');
        } catch (\Throwable $e) {
            error_log('[Health] DB ping failed: ' . $e->getMessage());
            Response::json([
                'status' => 'degraded',
                'time'   => gmdate('Y-m-d\TH:i:s\Z'),
            ], 503);
            return;
        }
        Response::json([
            'status' => 'ok',
            'time'   => gmdate('Y-m-d\TH:i:s\Z'),
        ]);
    }
}
