<?php

declare(strict_types=1);

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

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

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

$validated = !empty($databaseValidated);
?>
<div class="darlink-database-step">
    <div class="darlink-page-heading">
        <div>
            <span class="darlink-page-kicker">
                Connexion MariaDB / MySQL
            </span>

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

            <p>
                Saisissez les informations fournies par votre hébergeur.
                La base doit déjà exister avant de poursuivre.
            </p>
        </div>

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

            <div>
                <strong>
                    <?= $validated ? 'Connexion validée' : 'Connexion à tester' ?>
                </strong>

                <span>
                    <?= $validated
                        ? 'Les paramètres sont mémorisés pour la suite.'
                        : 'Testez la connexion avant de continuer.' ?>
                </span>
            </div>
        </div>
    </div>

    <?php if (!empty($databaseError)): ?>
        <div class="darlink-notice darlink-notice-danger">
            <i class="bi bi-x-circle-fill"></i>
            <span>
                <?= htmlspecialchars(
                    (string)$databaseError,
                    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 connexion</h3>

            <form
                method="post"
                action="index.php?step=3"
                class="darlink-install-form"
                autocomplete="off"
            >
                <div class="row g-3">
                    <div class="col-md-8">
                        <label class="darlink-form-label" for="db_host">
                            Serveur
                        </label>

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

                    <div class="col-md-4">
                        <label class="darlink-form-label" for="db_port">
                            Port
                        </label>

                        <input
                            type="number"
                            class="darlink-form-control"
                            id="db_port"
                            name="db_port"
                            min="1"
                            max="65535"
                            value="<?= (int)($form['db_port'] ?? 3306) ?>"
                            required
                        >
                    </div>

                    <div class="col-12">
                        <label class="darlink-form-label" for="db_database">
                            Nom de la base
                        </label>

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

                    <div class="col-12">
                        <label class="darlink-form-label" for="db_username">
                            Utilisateur
                        </label>

                        <input
                            type="text"
                            class="darlink-form-control"
                            id="db_username"
                            name="db_username"
                            value="<?= htmlspecialchars(
                                (string)($form['db_username'] ?? ''),
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>"
                            placeholder="Utilisateur MariaDB / MySQL"
                            required
                        >
                    </div>

                    <div class="col-12">
                        <label class="darlink-form-label" for="db_password">
                            Mot de passe
                        </label>

                        <div class="darlink-password-field">
                            <input
                                type="password"
                                class="darlink-form-control"
                                id="db_password"
                                name="db_password"
                                value="<?= htmlspecialchars(
                                    (string)($form['db_password'] ?? ''),
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>"
                                placeholder="Mot de passe de la base"
                            >

                            <button
                                type="button"
                                class="darlink-password-toggle"
                                data-password-toggle="db_password"
                                title="Afficher ou masquer le mot de passe"
                            >
                                <i class="bi bi-eye"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div class="darlink-form-help">
                    <i class="bi bi-info-circle"></i>

                    <span>
                        Sur LWS, ces informations sont disponibles dans
                        la gestion des bases MySQL / MariaDB.
                    </span>
                </div>

                <button
                    type="submit"
                    class="btn darlink-install-btn-primary w-100"
                >
                    <i class="bi bi-plug me-2"></i>
                    Tester la connexion
                </button>
            </form>
        </section>

        <section class="darlink-diagnostic-panel">
            <h3>Diagnostic</h3>

            <?php if ($diagnostics === []): ?>
                <div class="darlink-diagnostic-empty">
                    <i class="bi bi-database"></i>

                    <p>
                        Le résultat détaillé apparaîtra après le test.
                    </p>
                </div>
            <?php else: ?>
                <div class="darlink-check-list">
                    <?php foreach ($diagnostics as $diagnostic): ?>
                        <?php
                        $status = (string)(
                            $diagnostic['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)($diagnostic['label'] ?? ''),
                                            ENT_QUOTES,
                                            'UTF-8'
                                        ) ?>
                                    </strong>
                                </div>

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

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

    <div class="darlink-install-actions">
        <a
            href="index.php?step=2"
            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=4"
                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
            >
                Testez la connexion pour continuer
            </button>
        <?php endif; ?>
    </div>
</div>
