<?php

$roleLabels = [
    'ADMINISTRATEUR' => 'Administrateur',
    'COMMERCIAL' => 'Commercial',
    'MAGASIN' => 'Magasin',
    'LECTURE' => 'Lecture seule',
];

$roleBadges = [
    'ADMINISTRATEUR' => 'darlink-badge-danger',
    'COMMERCIAL' => 'darlink-badge-primary',
    'MAGASIN' => 'darlink-badge-success',
    'LECTURE' => 'darlink-badge-light',
];

$role = strtoupper(
    trim((string)($user['role'] ?? 'LECTURE'))
);

$fullName = trim(
    (string)($user['prenom'] ?? '')
    . ' '
    . (string)($user['nom'] ?? '')
);

if ($fullName === '') {
    $fullName = (string)($user['login'] ?? '');
}

$isActive = (int)($user['actif'] ?? 0) === 1;

$formatDateTime = static function (?string $value): string {
    $value = trim((string)$value);

    if ($value === '') {
        return 'Jamais';
    }

    $timestamp = strtotime($value);

    return $timestamp
        ? date('d/m/Y à H:i', $timestamp)
        : $value;
};
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-person-badge me-2"></i>
            Fiche utilisateur
        </h1>

        <p class="text-muted mb-0">
            Consulter les informations et les accès de l’utilisateur.
        </p>
    </div>

    <div class="d-flex gap-2">
        <a
            href="index.php?page=utilisateurs"
            class="btn btn-outline-secondary"
        >
            <i class="bi bi-arrow-left me-1"></i>
            Retour
        </a>

        <a
            href="index.php?page=utilisateurs_edit&id=<?= (int)$user['id'] ?>"
            class="btn darlink-btn-primary"
        >
            <i class="bi bi-pencil me-1"></i>
            Modifier
        </a>
    </div>
</div>

<?php if (!empty($success)): ?>
    <div class="alert alert-success">
        <?= htmlspecialchars($success) ?>
    </div>
<?php endif; ?>

<?php if (!empty($error)): ?>
    <div class="alert alert-danger">
        <?= htmlspecialchars($error) ?>
    </div>
<?php endif; ?>

<div class="row g-4">
    <div class="col-xl-8">
        <div class="darlink-card overflow-hidden">
            <div class="darlink-card-body">
                <div class="d-flex align-items-center gap-4 mb-4">
                    <div class="darlink-user-avatar darlink-user-avatar-large">
                        <?= htmlspecialchars(
                            strtoupper(
                                substr(
                                    trim(
                                        (string)($user['prenom'] ?? '')
                                        . (string)($user['nom'] ?? '')
                                    ),
                                    0,
                                    2
                                )
                            )
                        ) ?>
                    </div>

                    <div>
                        <h2 class="mb-2">
                            <?= htmlspecialchars($fullName) ?>
                        </h2>

                        <div class="d-flex flex-wrap gap-2">
                            <span class="darlink-badge <?= htmlspecialchars(
                                $roleBadges[$role]
                                    ?? 'darlink-badge-light'
                            ) ?>">
                                <?= htmlspecialchars(
                                    $roleLabels[$role] ?? $role
                                ) ?>
                            </span>

                            <?php if ($isActive): ?>
                                <span class="darlink-badge darlink-badge-success">
                                    Actif
                                </span>
                            <?php else: ?>
                                <span class="darlink-badge darlink-badge-danger">
                                    Inactif
                                </span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <div class="darlink-section-title">
                    Informations générales
                </div>

                <div class="row g-4">
                    <div class="col-md-6">
                        <div class="text-muted small mb-1">Nom</div>
                        <div class="fw-semibold">
                            <?= htmlspecialchars(
                                (string)($user['nom'] ?? '')
                            ) ?>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="text-muted small mb-1">Prénom</div>
                        <div class="fw-semibold">
                            <?= htmlspecialchars(
                                (string)($user['prenom'] ?? '')
                            ) ?: '—' ?>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="text-muted small mb-1">
                            Identifiant de connexion
                        </div>

                        <div class="darlink-table-code">
                            <?= htmlspecialchars(
                                (string)($user['login'] ?? '')
                            ) ?>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="text-muted small mb-1">
                            Adresse e-mail
                        </div>

                        <div class="fw-semibold">
                            <?= htmlspecialchars(
                                (string)($user['email'] ?? '')
                            ) ?: '—' ?>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="text-muted small mb-1">Rôle</div>
                        <div class="fw-semibold">
                            <?= htmlspecialchars(
                                $roleLabels[$role] ?? $role
                            ) ?>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="text-muted small mb-1">
                            État du compte
                        </div>

                        <div class="fw-semibold">
                            <?= $isActive ? 'Actif' : 'Inactif' ?>
                        </div>
                    </div>
                </div>

                <div class="darlink-section-title mt-4">
                    Suivi du compte
                </div>

                <div class="row g-4">
                    <div class="col-md-6">
                        <div class="text-muted small mb-1">
                            Dernière connexion
                        </div>

                        <div class="fw-semibold">
                            <?= htmlspecialchars(
                                $formatDateTime(
                                    (string)($user['derniere_connexion'] ?? '')
                                )
                            ) ?>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="text-muted small mb-1">
                            Date de création
                        </div>

                        <div class="fw-semibold">
                            <?= htmlspecialchars(
                                $formatDateTime(
                                    (string)($user['date_creation'] ?? '')
                                )
                            ) ?>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="text-muted small mb-1">
                            Dernière modification
                        </div>

                        <div class="fw-semibold">
                            <?= htmlspecialchars(
                                $formatDateTime(
                                    (string)($user['date_modification'] ?? '')
                                )
                            ) ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-xl-4">
        <div class="darlink-card overflow-hidden">
            <div class="darlink-card-header">
                Actions
            </div>

            <div class="darlink-card-body d-grid gap-3">
                <a
                    href="index.php?page=utilisateurs_edit&id=<?= (int)$user['id'] ?>"
                    class="btn darlink-btn-primary"
                >
                    <i class="bi bi-pencil me-1"></i>
                    Modifier l’utilisateur
                </a>

                <a
                    href="index.php?page=utilisateurs_password&id=<?= (int)$user['id'] ?>"
                    class="btn btn-outline-primary"
                >
                    <i class="bi bi-key me-1"></i>
                    Changer le mot de passe
                </a>

                <?php if ($isActive): ?>
                    <form
                        method="post"
                        action="index.php?page=utilisateurs_deactivate"
                        onsubmit="return confirm('Désactiver cet utilisateur ?');"
                    >
                        <input
                            type="hidden"
                            name="id"
                            value="<?= (int)$user['id'] ?>"
                        >

                        <button
                            type="submit"
                            class="btn btn-outline-danger w-100"
                        >
                            <i class="bi bi-person-x me-1"></i>
                            Désactiver le compte
                        </button>
                    </form>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>