<?php
declare(strict_types=1);
namespace App\Controllers;

use App\Core\Auth;
use App\Core\Controller;
use App\Core\Csrf;
use App\Core\Permission;
use App\Platform\Health\HealthMonitorService;

final class HealthMonitorController extends Controller
{
    public function index(): void
    {
        Auth::requireLogin();
        Permission::require('system.health.view');
        $this->view('system/health_monitor', [
            'pageTitle' => 'Health Monitor PRO',
            'report' => (new HealthMonitorService())->report(),
        ]);
    }

    public function run(): void
    {
        Auth::requireLogin();
        Permission::require('system.health.manage');
        Csrf::verify($_POST['_csrf'] ?? null);
        (new HealthMonitorService())->report(true);
        $_SESSION['success'] = 'Analyse Health Monitor PRO terminée.';
        $this->redirect('/administration/systeme/sante');
    }

    public function baseline(): void
    {
        Auth::requireLogin();
        Permission::require('system.health.manage');
        Csrf::verify($_POST['_csrf'] ?? null);
        $count = (new HealthMonitorService())->createIntegrityBaseline();
        $_SESSION['success'] = $count . ' fichier(s) ajouté(s) à la baseline d’intégrité.';
        $this->redirect('/administration/systeme/sante');
    }

    public function acknowledge(): void
    {
        Auth::requireLogin();
        Permission::require('system.health.manage');
        Csrf::verify($_POST['_csrf'] ?? null);
        (new HealthMonitorService())->acknowledgeAlerts();
        $_SESSION['success'] = 'Alertes de santé acquittées.';
        $this->redirect('/administration/systeme/sante');
    }
}
