<?php

declare(strict_types=1);

$checks = is_array($requirements['checks'] ?? null)
    ? $requirements['checks']
    : [];

$groups = [];

foreach ($checks as $check) {
    $group = (string)($check['group'] ?? 'Autres');
    $groups[$group][] = $check;
}

$ready = !empty($requirements['ready']);
$requiredFailures = (int)($requirements['required_failures'] ?? 0);
$recommendedWarnings = (int)($requirements['recommended_warnings'] ?? 0);
?>
<div class="darlink-requirements">
    <div class="darlink-page-heading">
        <div>
            <span class="darlink-page-kicker">
                Contrôle automatique
            </span>

            <h2>Vérification du serveur</h2>

            <p>
                Darlink ERP contrôle la version de PHP, les extensions
                et les permissions nécessaires à son installation.
            </p>
        </div>

        <div class="darlink-readiness <?= $ready ? 'is-ready' : 'has-errors' ?>">
            <i class="bi <?= $ready ? 'bi-check-circle-fill' : 'bi-exclamation-triangle-fill' ?>"></i>

            <div>
                <strong>
                    <?= $ready ? 'Serveur compatible' : 'Corrections nécessaires' ?>
                </strong>

                <span>
                    <?php if ($ready): ?>
                        Vous pouvez continuer l’installation.
                    <?php else: ?>
                        <?= $requiredFailures ?> contrôle(s) obligatoire(s) en échec.
                    <?php endif; ?>
                </span>
            </div>
        </div>
    </div>

    <?php if ($recommendedWarnings > 0): ?>
        <div class="darlink-notice darlink-notice-warning">
            <i class="bi bi-info-circle-fill"></i>
            <span>
                <?= $recommendedWarnings ?>
                extension(s) recommandée(s) ne sont pas disponibles.
                L’installation peut continuer, mais certaines fonctions
                pourront être limitées.
            </span>
        </div>
    <?php endif; ?>

    <?php foreach ($groups as $groupName => $groupChecks): ?>
        <section class="darlink-check-group">
            <h3><?= htmlspecialchars($groupName, ENT_QUOTES, 'UTF-8') ?></h3>

            <div class="darlink-check-list">
                <?php foreach ($groupChecks as $check): ?>
                    <?php
                    $status = (string)($check['status'] ?? 'error');

                    $icon = match ($status) {
                        'success' => 'bi-check-circle-fill',
                        'warning' => 'bi-exclamation-circle-fill',
                        default => 'bi-x-circle-fill',
                    };
                    ?>

                    <article class="darlink-check-item is-<?= htmlspecialchars($status, ENT_QUOTES, 'UTF-8') ?>">
                        <div class="darlink-check-icon">
                            <i class="bi <?= $icon ?>"></i>
                        </div>

                        <div class="darlink-check-content">
                            <div class="darlink-check-title">
                                <strong>
                                    <?= htmlspecialchars(
                                        (string)($check['label'] ?? ''),
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>
                                </strong>

                                <?php if (!empty($check['required'])): ?>
                                    <span class="darlink-check-required">
                                        Obligatoire
                                    </span>
                                <?php else: ?>
                                    <span class="darlink-check-recommended">
                                        Recommandé
                                    </span>
                                <?php endif; ?>
                            </div>

                            <p>
                                <?= htmlspecialchars(
                                    (string)($check['message'] ?? ''),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>
                            </p>
                        </div>

                        <div class="darlink-check-value">
                            <?= htmlspecialchars(
                                (string)($check['value'] ?? ''),
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        </section>
    <?php endforeach; ?>

    <div class="darlink-install-actions">
        <a
            href="index.php?step=1"
            class="btn darlink-install-btn-secondary"
        >
            <i class="bi bi-arrow-left me-2"></i>
            Retour
        </a>

        <div class="d-flex gap-2 flex-wrap justify-content-end">
            <a
                href="index.php?step=2&refresh=<?= time() ?>"
                class="btn darlink-install-btn-secondary"
            >
                <i class="bi bi-arrow-clockwise me-2"></i>
                Vérifier à nouveau
            </a>

            <?php if ($ready): ?>
                <a
                    href="index.php?step=3"
                    class="btn darlink-install-btn-primary"
                >
                    Continuer
                    <i class="bi bi-arrow-right ms-2"></i>
                </a>
            <?php else: ?>
                <button
                    type="button"
                    class="btn darlink-install-btn-primary"
                    disabled
                >
                    Corrigez les erreurs pour continuer
                </button>
            <?php endif; ?>
        </div>
    </div>
</div>
