<?php
declare(strict_types=1);

namespace App\Modules\Api\Controllers\Api;

use App\Core\ApiController;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;

final class HealthController extends ApiController
{
    public function check(Request $request): Response
    {
        $dbOk = false;
        try {
            $db = App::getInstance()->db();
            if ($db) {
                $db->selectOne("SELECT 1");
                $dbOk = true;
            }
        } catch (\Throwable $e) {
        }

        return $this->successResponse([
            'status' => $dbOk ? 'ok' : 'degraded',
            'timestamp' => date('c'),
            'services' => [
                'database' => $dbOk ? 'ok' : 'down',
            ],
        ]);
    }
}
