<?php
use App\Controllers\PublicController;

/** GET /api/health — public health round-trip (proves DB connectivity). */
class Health extends PublicController
{
    public function index()
    {
        $this->guardPublic('health', 120, 60);
        try {
            $up   = (int) \Db::getValue("SELECT 1");
            $rows = (int) \Db::getValue("SELECT COUNT(*) FROM `config`");
        } catch (\Throwable $e) {
            $this->json(null, 500, ['code' => 'db_error', 'message' => DEBUG ? $e->getMessage() : 'Database unavailable']);
        }
        $this->json([
            'db'          => $up === 1 ? 'connected' : 'unknown',
            'app'         => defined('PROJECT_NAME') ? PROJECT_NAME : '',
            'config_rows' => $rows,
            'time'        => date('c'),
        ]);
    }
}
