<?php

declare(strict_types=1);

$report = is_array($installationReport ?? null)
    ? $installationReport
    : [];

$summary = is_array($report['summary'] ?? null)
    ? $report['summary']
    : [];

$sql = is_array($report['sql'] ?? null)
    ? $report['sql']
    : [];

$databaseState = is_array(
    $report['database_state'] ?? null
)
    ? $report['database_state']
    : [];

$groups = is_array($report['groups'] ?? null)
    ? $report['groups']
    : [];

$ready = !empty($report['ready']);
$existingInstallation = !empty(
    $databaseState['existing_installation']
);
?>
<div class="darlink-preparation-step">
    <div class="darlink-page-heading">
        <div>
            <span class="darlink-page-kicker">
                Audit final
            </span>

            <h2>Préparation de l’installation</h2>

            <p>
                Vérifiez le récapitulatif avant toute modification
                de la base de données.
            </p>
        </div>

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

            <div>
                <strong>
                    <?= $ready
                        ? 'Installation prête'
                        : 'Contrôles à corriger' ?>
                </strong>

                <span>
                    <?= $ready
                        ? 'Tous les contrôles obligatoires sont conformes.'
                        : (int)($report['blocking_failures'] ?? 0)
                            . ' contrôle(s) bloquant(s).' ?>
                </span>
            </div>
        </div>
    </div>

    <?php if (!empty($preparationError)): ?>
        <div class="darlink-notice darlink-notice-danger">
            <i class="bi bi-x-circle-fill"></i>
            <span>
                <?= htmlspecialchars(
                    (string)$preparationError,
                    ENT_QUOTES,
                    'UTF-8'
                ) ?>
            </span>
        </div>
    <?php endif; ?>

    <?php if ($existingInstallation): ?>
        <div class="darlink-notice darlink-notice-warning">
            <i class="bi bi-exclamation-triangle-fill"></i>

            <span>
                Une installation Darlink ERP semble déjà présente
                dans cette base. Une sauvegarde est vivement recommandée
                avant de poursuivre.
            </span>
        </div>
    <?php elseif (!empty($databaseState['tables'])): ?>
        <div class="darlink-notice darlink-notice-warning">
            <i class="bi bi-info-circle-fill"></i>

            <span>
                La base contient déjà
                <?= (int)$databaseState['tables'] ?>
                table(s), sans installation Darlink ERP clairement détectée.
            </span>
        </div>
    <?php endif; ?>

    <div class="darlink-summary-grid">
        <section class="darlink-summary-card">
            <div class="darlink-summary-icon">
                <i class="bi bi-box-seam"></i>
            </div>

            <h3>Application</h3>

            <dl>
                <div>
                    <dt>Nom</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['application'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>

                <div>
                    <dt>Édition</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['edition'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>

                <div>
                    <dt>Version</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['version'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>

                <div>
                    <dt>URL</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['url'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>

                <div>
                    <dt>Fuseau horaire</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['timezone'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>
            </dl>
        </section>

        <section class="darlink-summary-card">
            <div class="darlink-summary-icon">
                <i class="bi bi-database"></i>
            </div>

            <h3>Base de données</h3>

            <dl>
                <div>
                    <dt>Serveur</dt>
                    <dd>
                        <?= htmlspecialchars(
                            (string)($summary['db_host'] ?? ''),
                            ENT_QUOTES,
                            'UTF-8'
                        ) ?>:
                        <?= (int)($summary['db_port'] ?? 3306) ?>
                    </dd>
                </div>

                <div>
                    <dt>Base</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['db_database'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>

                <div>
                    <dt>Moteur</dt>
                    <dd>
                        <?= htmlspecialchars(
                            (string)($summary['db_product'] ?? ''),
                            ENT_QUOTES,
                            'UTF-8'
                        ) ?>
                        <?= htmlspecialchars(
                            (string)($summary['db_version'] ?? ''),
                            ENT_QUOTES,
                            'UTF-8'
                        ) ?>
                    </dd>
                </div>

                <div>
                    <dt>Encodage</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['db_charset'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>

                <div>
                    <dt>Collation</dt>
                    <dd><?= htmlspecialchars(
                        (string)($summary['db_collation'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?></dd>
                </div>
            </dl>
        </section>

        <section class="darlink-summary-card">
            <div class="darlink-summary-icon">
                <i class="bi bi-filetype-sql"></i>
            </div>

            <h3>Contenu à installer</h3>

            <div class="darlink-estimation">
                <div>
                    <strong><?= (int)($sql['files_count'] ?? 0) ?></strong>
                    <span>Fichier(s) SQL</span>
                </div>

                <div>
                    <strong><?= (int)($sql['tables'] ?? 0) ?></strong>
                    <span>Tables détectées</span>
                </div>

                <div>
                    <strong><?= (int)($sql['indexes'] ?? 0) ?></strong>
                    <span>Index détectés</span>
                </div>

                <div>
                    <strong><?= (int)($sql['constraints'] ?? 0) ?></strong>
                    <span>Contraintes</span>
                </div>

                <div>
                    <strong><?= (int)($sql['inserts'] ?? 0) ?></strong>
                    <span>Blocs de données</span>
                </div>

                <div>
                    <strong>
                        ≈ <?= (int)($sql['estimated_seconds'] ?? 0) ?> s
                    </strong>
                    <span>Durée estimée</span>
                </div>
            </div>
        </section>

        <section class="darlink-summary-card">
            <div class="darlink-summary-icon">
                <i class="bi bi-hdd-stack"></i>
            </div>

            <h3>État de la base</h3>

            <div class="darlink-database-state">
                <span class="darlink-state-badge <?= $existingInstallation
                    ? 'is-warning'
                    : 'is-success' ?>">
                    <?= htmlspecialchars(
                        (string)($databaseState['display'] ?? ''),
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>
                </span>

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

    <section class="darlink-audit-section">
        <h3>Audit final</h3>

        <?php foreach ($groups as $groupName => $groupChecks): ?>
            <div class="darlink-audit-group">
                <h4><?= htmlspecialchars(
                    (string)$groupName,
                    ENT_QUOTES,
                    'UTF-8'
                ) ?></h4>

                <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>
                                </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>
            </div>
        <?php endforeach; ?>
    </section>

    <form
        method="post"
        action="index.php?step=5"
        class="darlink-confirmation-panel darlink-install-form"
    >
        <label class="darlink-confirmation-item">
            <input
                type="checkbox"
                name="confirm_information"
                value="1"
                required
            >

            <span>
                J’ai vérifié les informations ci-dessus.
            </span>
        </label>

        <label class="darlink-confirmation-item">
            <input
                type="checkbox"
                name="confirm_installation"
                value="1"
                required
            >

            <span>
                Je confirme vouloir installer Darlink ERP
                dans cette base de données.
            </span>
        </label>

        <?php if ($existingInstallation): ?>
            <label class="darlink-confirmation-item is-warning">
                <input
                    type="checkbox"
                    name="confirm_existing"
                    value="1"
                    required
                >

                <span>
                    Je comprends qu’une installation existante
                    a été détectée et je souhaite poursuivre.
                </span>
            </label>
        <?php endif; ?>

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

            <button
                type="submit"
                class="btn darlink-install-btn-primary"
                <?= $ready ? '' : 'disabled' ?>
            >
                <i class="bi bi-play-circle me-2"></i>
                Préparer l’installation
            </button>
        </div>
    </form>
</div>
