<?php

declare(strict_types=1);

$form = is_array($form ?? null)
    ? $form
    : [];

$result = is_array($environmentResult ?? null)
    ? $environmentResult
    : null;

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

$validated = !empty($environmentValidated);
?>
<div class="darlink-environment-step">
    <div class="darlink-page-heading">
        <div>
            <span class="darlink-page-kicker">
                Configuration automatique
            </span>

            <h2>Environnement de production</h2>

            <p>
                L’assistant va créer le fichier .env,
                générer une clé de sécurité et enregistrer
                la connexion à la base de données.
            </p>
        </div>

        <div class="darlink-readiness <?= $validated ? 'is-ready' : 'has-errors' ?>">
            <i class="bi <?= $validated ? 'bi-file-earmark-check-fill' : 'bi-file-earmark-code-fill' ?>"></i>

            <div>
                <strong>
                    <?= $validated ? 'Configuration validée' : 'Configuration à générer' ?>
                </strong>

                <span>
                    <?= $validated
                        ? 'Le fichier .env est prêt.'
                        : 'Vérifiez les informations puis lancez la génération.' ?>
                </span>
            </div>
        </div>
    </div>

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

    <?php if ($result !== null): ?>
        <div class="darlink-notice <?= !empty($result['success'])
            ? 'darlink-notice-success'
            : 'darlink-notice-danger' ?>">
            <i class="bi <?= !empty($result['success'])
                ? 'bi-check-circle-fill'
                : 'bi-x-circle-fill' ?>"></i>

            <span>
                <?= htmlspecialchars(
                    (string)($result['message'] ?? ''),
                    ENT_QUOTES,
                    'UTF-8'
                ) ?>
            </span>
        </div>
    <?php endif; ?>

    <div class="darlink-database-layout">
        <section class="darlink-form-panel">
            <h3>Paramètres de l’application</h3>

            <form
                method="post"
                action="index.php?step=4"
                class="darlink-install-form"
                autocomplete="off"
            >
                <div class="row g-3">
                    <div class="col-12">
                        <label class="darlink-form-label" for="app_name">
                            Nom de l’application
                        </label>

                        <input
                            type="text"
                            class="darlink-form-control"
                            id="app_name"
                            name="app_name"
                            value="<?= htmlspecialchars(
                                (string)($form['app_name'] ?? 'Darlink ERP'),
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>"
                            required
                        >
                    </div>

                    <div class="col-12">
                        <label class="darlink-form-label" for="app_url">
                            Adresse publique
                        </label>

                        <input
                            type="url"
                            class="darlink-form-control"
                            id="app_url"
                            name="app_url"
                            value="<?= htmlspecialchars(
                                (string)($form['app_url'] ?? ''),
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>"
                            placeholder="https://erp.mondomaine.com"
                            required
                        >
                    </div>

                    <div class="col-md-5">
                        <label class="darlink-form-label" for="app_locale">
                            Langue
                        </label>

                        <select
                            class="darlink-form-control"
                            id="app_locale"
                            name="app_locale"
                            required
                        >
                            <option
                                value="fr"
                                <?= (($form['app_locale'] ?? 'fr') === 'fr')
                                    ? 'selected'
                                    : '' ?>
                            >
                                Français
                            </option>
                        </select>
                    </div>

                    <div class="col-md-7">
                        <label class="darlink-form-label" for="app_timezone">
                            Fuseau horaire
                        </label>

                        <select
                            class="darlink-form-control"
                            id="app_timezone"
                            name="app_timezone"
                            required
                        >
                            <?php foreach ([
                                'Africa/Casablanca' => 'Maroc — Casablanca',
                                'Europe/Paris' => 'France — Paris',
                                'UTC' => 'UTC',
                            ] as $timezone => $label): ?>
                                <option
                                    value="<?= htmlspecialchars(
                                        $timezone,
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>"
                                    <?= (($form['app_timezone'] ?? '') === $timezone)
                                        ? 'selected'
                                        : '' ?>
                                >
                                    <?= htmlspecialchars(
                                        $label,
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="darlink-form-help">
                    <i class="bi bi-shield-lock"></i>

                    <span>
                        Une clé APP_KEY unique sera générée automatiquement.
                        Ne modifiez plus cette clé après l’installation.
                    </span>
                </div>

                <button
                    type="submit"
                    class="btn darlink-install-btn-primary w-100"
                >
                    <i class="bi bi-file-earmark-code me-2"></i>
                    Générer la configuration
                </button>
            </form>
        </section>

        <section class="darlink-diagnostic-panel">
            <h3>Contrôles du fichier .env</h3>

            <?php if ($checks === []): ?>
                <div class="darlink-diagnostic-empty">
                    <i class="bi bi-file-earmark-code"></i>

                    <p>
                        Les contrôles apparaîtront après la génération.
                    </p>
                </div>
            <?php else: ?>
                <div class="darlink-check-list">
                    <?php foreach ($checks as $check): ?>
                        <?php
                        $status = (string)(
                            $check['status'] ?? 'error'
                        );

                        $icon = $status === 'success'
                            ? 'bi-check-circle-fill'
                            : '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>

                <?php if (!empty($result['backup_path'])): ?>
                    <div class="darlink-form-help mt-3 mb-0">
                        <i class="bi bi-shield-check"></i>

                        <span>
                            Une copie de sécurité de l’ancien fichier .env
                            a été créée automatiquement.
                        </span>
                    </div>
                <?php endif; ?>
            <?php endif; ?>
        </section>
    </div>

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

        <?php if ($validated): ?>
            <a
                href="index.php?step=5"
                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
            >
                Générez la configuration pour continuer
            </button>
        <?php endif; ?>
    </div>
</div>
