<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Auth;
use App\Foundation\Foundation;
use App\Foundation\HealthReport;
use App\Core\Permission;

final class FoundationController
{
    public function index(): void
    {
        $this->guard();
        $app = Foundation::app();
        $pageTitle = 'AXIOM V8 Foundation';
        $modules = $app->modules()->all();
        $entities = $app->metadata()->all();
        $version = $app->version();
        require BASE_PATH . '/app/Views/foundation/index.php';
    }

    public function diagnostic(): void
    {
        $this->guard();
        $app = Foundation::app();
        /** @var HealthReport $report */
        $report = $app->container()->get(HealthReport::class);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode([
            'platform' => 'AXIOM',
            'version' => $app->version(),
            'generated_at' => date(DATE_ATOM),
            'checks' => $report->checks(),
        ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

    private function guard(): void
    {
        if (!Auth::check()) {
            header('Location: /public/login');
            exit;
        }
        Permission::require('axiom.kernel.view');
    }
}
