<?php
$title = 'Mon profil';
$base  = rtrim(App\Core\Config::baseUrl(), '/');

$roleColors = [
    'admin'       => ['bg' => '#fef2f2', 'color' => '#b91c1c', 'icon' => 'fa-crown'],
    'manager'     => ['bg' => '#faf5ff', 'color' => '#7e22ce', 'icon' => 'fa-briefcase'],
    'superviseur' => ['bg' => '#fff7ed', 'color' => '#c2410c', 'icon' => 'fa-binoculars'],
    'supervisor'  => ['bg' => '#fff7ed', 'color' => '#c2410c', 'icon' => 'fa-binoculars'],
    'technicien'  => ['bg' => '#f0fdf4', 'color' => '#15803d', 'icon' => 'fa-screwdriver-wrench'],
    'agent'       => ['bg' => '#eff6ff', 'color' => '#1d4ed8', 'icon' => 'fa-headset'],
];
$defaultRole = ['bg' => '#f1f5f9', 'color' => '#475569', 'icon' => 'fa-user'];
?>

<?php if (!$user): ?>
  <div class="alert alert-danger d-flex align-items-center gap-2">
    <i class="fas fa-circle-exclamation fa-fw"></i>
    <span>Utilisateur introuvable.</span>
  </div>
<?php else:
  $role     = $user['role_key'] ?? 'agent';
  $technicianType = trim((string)($user['technician_type'] ?? ''));
  $assignedCity = trim((string)($user['assigned_city'] ?? ''));
  $technicianTypeLabels = [
    'etude_raccordement' => 'Etude & Raccordement',
    'raccordement' => 'Raccordement',
    'backbones' => 'Backbones',
    'maintenance_ftth' => 'Maintenance FTTH',
    'etude' => 'Etude',
  ];
  $rc       = $roleColors[$role] ?? $defaultRole;
  $initials = mb_strtoupper(mb_substr($user['name'] ?? '?', 0, 2));
  $isActive = (int)$user['active'] === 1;
  $avatarPalette = ['#6366f1','#0ea5e9','#10b981','#f59e0b','#ef4444','#8b5cf6','#14b8a6'];
  $avatarBg = $avatarPalette[$user['id'] % count($avatarPalette)];
  $hasTakeover = !empty($user['takeover_at']);
?>

<div class="row g-4">

  <!-- Colonne principale -->
  <div class="col-12 col-lg-8">

    <!-- Carte identité -->
    <div class="card border-0 shadow-sm mb-4" style="border-radius:1rem;">
      <div class="card-body p-4">
        <div class="d-flex align-items-center gap-4 flex-wrap">
          <!-- Avatar -->
          <div class="rounded-circle d-flex align-items-center justify-content-center fw-bold text-white flex-shrink-0"
               style="width:72px;height:72px;background:<?= htmlspecialchars($avatarBg) ?>;font-size:1.5rem;letter-spacing:-.02em;">
            <?= htmlspecialchars($initials) ?>
          </div>
          <!-- Infos -->
          <div class="flex-grow-1 min-w-0">
            <h4 class="fw-bold mb-1" style="letter-spacing:-.02em;"><?= htmlspecialchars($user['name']) ?></h4>
            <p class="text-muted mb-2" style="font-size:.9rem;"><?= htmlspecialchars($user['email']) ?></p>
            <div class="d-flex flex-wrap gap-2">
              <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                    style="font-size:.78rem;background:<?= htmlspecialchars($rc['bg']) ?>;color:<?= htmlspecialchars($rc['color']) ?>;">
                <i class="fas <?= htmlspecialchars($rc['icon']) ?>" style="font-size:.72rem;"></i>
                <?= htmlspecialchars(ucfirst($role)) ?>
              </span>
              <?php if ($technicianType !== ''): ?>
                <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                      style="font-size:.78rem;background:#ecfeff;color:#0f766e;">
                  <i class="fas fa-layer-group" style="font-size:.72rem;"></i>
                  <?= htmlspecialchars($technicianTypeLabels[$technicianType] ?? ucfirst(str_replace('_', ' ', $technicianType))) ?>
                </span>
              <?php endif; ?>
              <?php if ($assignedCity !== ''): ?>
                <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                      style="font-size:.78rem;background:#fff7ed;color:#c2410c;">
                  <i class="fas fa-city" style="font-size:.72rem;"></i>
                  <?= htmlspecialchars($assignedCity) ?>
                </span>
              <?php endif; ?>
              <?php if ($isActive): ?>
                <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                      style="font-size:.78rem;background:#f0fdf4;color:#15803d;">
                  <span style="width:6px;height:6px;border-radius:50%;background:#16a34a;display:inline-block;"></span>
                  Actif
                </span>
              <?php else: ?>
                <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                      style="font-size:.78rem;background:#fef2f2;color:#b91c1c;">
                  <span style="width:6px;height:6px;border-radius:50%;background:#dc2626;display:inline-block;"></span>
                  Inactif
                </span>
              <?php endif; ?>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Carte prise en charge (technicien / admin) -->
    <?php if (in_array($role, ['technicien','admin'])): ?>
    <div class="card border-0 shadow-sm" style="border-radius:1rem;">
      <div class="card-body p-4">
        <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
          <div>
            <h6 class="fw-bold mb-1">Prise en charge</h6>
            <p class="text-muted small mb-0">Enregistre votre heure de début d'intervention.</p>
          </div>
          <?php if ($hasTakeover): ?>
            <span class="badge rounded-pill bg-success-subtle text-success px-3 py-2" style="font-size:.8rem;">
              <i class="fas fa-circle-check me-1"></i> Enregistrée
            </span>
          <?php endif; ?>
        </div>

        <hr class="my-3">

        <?php if ($hasTakeover): ?>
          <div class="d-flex align-items-center gap-3">
            <div class="rounded-3 d-flex align-items-center justify-content-center flex-shrink-0"
                 style="width:44px;height:44px;background:#f0fdf4;">
              <i class="fas fa-calendar-check" style="color:#15803d;font-size:1.1rem;"></i>
            </div>
            <div>
              <div class="fw-semibold"><?= date('d/m/Y', strtotime($user['takeover_at'])) ?></div>
              <div class="text-muted small"><?= date('H\hi', strtotime($user['takeover_at'])) ?></div>
            </div>
          </div>
          <p class="text-muted small mt-3 mb-0">
            <i class="fas fa-lock me-1"></i>
            La prise en charge est déjà enregistrée et ne peut plus être modifiée.
          </p>
        <?php else: ?>
          <form method="post" action="<?= htmlspecialchars(route_url('/users/takeover')) ?>"
                onsubmit="this.querySelector('button').disabled=true;">
            <p class="text-muted small mb-3">
              Aucune prise en charge enregistrée pour aujourd'hui. Cliquez sur le bouton ci-dessous pour horodater votre démarrage.
            </p>
            <button type="submit" class="btn btn-primary d-inline-flex align-items-center gap-2">
              <i class="fas fa-play"></i> Démarrer la prise en charge
            </button>
          </form>
        <?php endif; ?>
      </div>
    </div>
    <?php endif; ?>

  </div>

  <!-- Colonne latérale -->
  <div class="col-12 col-lg-4">
    <div class="card border-0 shadow-sm" style="border-radius:1rem;">
      <div class="card-body p-4">
        <h6 class="fw-bold mb-3">Détails du compte</h6>
        <dl class="mb-0" style="font-size:.875rem;">
          <dt class="text-muted fw-normal mb-1">Identifiant</dt>
          <dd class="fw-semibold mb-3">#<?= (int)$user['id'] ?></dd>

          <dt class="text-muted fw-normal mb-1">Nom</dt>
          <dd class="fw-semibold mb-3"><?= htmlspecialchars($user['name']) ?></dd>

          <dt class="text-muted fw-normal mb-1">Email</dt>
          <dd class="fw-semibold mb-3" style="word-break:break-all;"><?= htmlspecialchars($user['email']) ?></dd>

          <dt class="text-muted fw-normal mb-1">Rôle</dt>
          <dd class="mb-3">
            <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                  style="font-size:.78rem;background:<?= htmlspecialchars($rc['bg']) ?>;color:<?= htmlspecialchars($rc['color']) ?>;">
              <i class="fas <?= htmlspecialchars($rc['icon']) ?>" style="font-size:.72rem;"></i>
              <?= htmlspecialchars(ucfirst($role)) ?>
            </span>
          </dd>

          <?php if ($technicianType !== ''): ?>
          <dt class="text-muted fw-normal mb-1">Type métier</dt>
          <dd class="fw-semibold mb-3"><?= htmlspecialchars($technicianTypeLabels[$technicianType] ?? ucfirst(str_replace('_', ' ', $technicianType))) ?></dd>
          <?php endif; ?>

          <?php if ($assignedCity !== ''): ?>
          <dt class="text-muted fw-normal mb-1">Ville de rattachement</dt>
          <dd class="fw-semibold mb-3"><?= htmlspecialchars($assignedCity) ?></dd>
          <?php endif; ?>

          <dt class="text-muted fw-normal mb-1">Statut</dt>
          <dd class="mb-0">
            <?php if ($isActive): ?>
              <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                    style="font-size:.78rem;background:#f0fdf4;color:#15803d;">
                <span style="width:6px;height:6px;border-radius:50%;background:#16a34a;display:inline-block;"></span>
                Actif
              </span>
            <?php else: ?>
              <span class="d-inline-flex align-items-center gap-1 px-2 py-1 rounded-pill fw-semibold"
                    style="font-size:.78rem;background:#fef2f2;color:#b91c1c;">
                <span style="width:6px;height:6px;border-radius:50%;background:#dc2626;display:inline-block;"></span>
                Inactif
              </span>
            <?php endif; ?>
          </dd>
        </dl>
      </div>
    </div>
  </div>

</div>
<?php endif; ?>
