<?php
require_once __DIR__ . '/../includes/config.php';
requireLogin();
$db = getDB();

// Handle logout
if (isset($_GET['logout'])) {
    session_destroy();
    redirect(SITE_URL . '/admin/login');
}

// Fetch Comprehensive Statistics
try {
    // Basic counts
    $stats = [
        'properties'     => $db->query("SELECT COUNT(*) FROM properties WHERE publication_status = 'publie'")->fetchColumn(),
        'properties_sale'=> $db->query("SELECT COUNT(*) FROM properties WHERE status = 'vente' AND publication_status = 'publie'")->fetchColumn(),
        'properties_rent'=> $db->query("SELECT COUNT(*) FROM properties WHERE status = 'location' AND publication_status = 'publie'")->fetchColumn(),
        'new_contacts'   => $db->query("SELECT COUNT(*) FROM contact_messages WHERE status = 'nouveau'")->fetchColumn(),
        'total_contacts' => $db->query("SELECT COUNT(*) FROM contact_messages")->fetchColumn(),
        'new_estims'     => $db->query("SELECT COUNT(*) FROM estimation_requests WHERE status = 'nouveau'")->fetchColumn(),
        'total_estims'   => $db->query("SELECT COUNT(*) FROM estimation_requests")->fetchColumn(),
        'rdv_upcoming'   => $db->query("SELECT COUNT(*) FROM rdv_requests WHERE date_rdv >= CURDATE() AND status = 'confirme'")->fetchColumn(),
        'total_rdvs'     => $db->query("SELECT COUNT(*) FROM rdv_requests")->fetchColumn(),
    ];

    // Visitor Statistics
    $visitorStats = getVisitorStats();

    // Calculate Ratios for Statistics Section
    $totalLeads = max(1, $stats['total_contacts'] + $stats['total_estims'] + $stats['total_rdvs']);
    $pctContacts = round(($stats['total_contacts'] / $totalLeads) * 100);
    $pctEstims   = round(($stats['total_estims'] / $totalLeads) * 100);
    $pctRdvs     = round(($stats['total_rdvs'] / $totalLeads) * 100);

    $totalPropsCount = max(1, $stats['properties']);
    $pctSale = round(($stats['properties_sale'] / $totalPropsCount) * 100);
    $pctRent = round(($stats['properties_rent'] / $totalPropsCount) * 100);

    // Recent Activity Records
    $recentContacts = $db->query("SELECT id, nom, email, profil_je_suis, status, created_at FROM contact_messages ORDER BY created_at DESC LIMIT 5")->fetchAll();
    $recentEstims   = $db->query("SELECT id, nom, email, type_bien, status, created_at FROM estimation_requests ORDER BY created_at DESC LIMIT 5")->fetchAll();
    $nextRdvs       = $db->query("SELECT id, prenom, nom, email, date_rdv, heure, status FROM rdv_requests WHERE date_rdv >= CURDATE() ORDER BY date_rdv ASC, heure ASC LIMIT 5")->fetchAll();

} catch(Exception $e) {
    $stats = [
        'properties'=>0, 'properties_sale'=>0, 'properties_rent'=>0, 
        'new_contacts'=>0, 'total_contacts'=>0,
        'new_estims'=>0, 'total_estims'=>0,
        'rdv_upcoming'=>0, 'total_rdvs'=>0
    ];
    $visitorStats = [
        'total_views' => 0, 'unique_visitors' => 0,
        'views_today' => 0, 'visitors_today' => 0,
        'views_month' => 0, 'visitors_month' => 0,
        'top_pages' => [], 'recent' => [], 'chart_data' => []
    ];
    $pctContacts = $pctEstims = $pctRdvs = $pctSale = $pctRent = 0;
    $recentContacts = $recentEstims = $nextRdvs = [];
}

$pageTitle = 'Tableau de Bord & Statistiques'; 
$activeNav = 'dashboard';
include __DIR__ . '/layout.php';
?>

<!-- VIBRANT STATS SUMMARY GRID -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-10">

  <!-- 1. Properties Card -->
  <a href="<?= SITE_URL ?>/admin/properties" class="admin-glass-card rounded-2xl p-6 relative overflow-hidden group hover:shadow-xl transition-all duration-300 transform hover:-translate-y-1">
    <div class="absolute top-0 right-0 w-28 h-28 bg-emerald-500/10 rounded-full blur-2xl group-hover:bg-emerald-500/20 transition-all"></div>
    <div class="flex items-center justify-between mb-4">
      <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-emerald-500 to-teal-600 text-white flex items-center justify-center shadow-lg shadow-emerald-500/30">
        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.75"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25"/></svg>
      </div>
      <span class="text-[10px] uppercase font-bold tracking-widest px-2.5 py-1 bg-emerald-50 text-emerald-700 rounded-full border border-emerald-200">Catalogue</span>
    </div>
    <div class="text-3xl font-extrabold text-slate-900 mb-1 font-sans"><?= $stats['properties'] ?></div>
    <div class="text-xs font-semibold text-slate-500 uppercase tracking-wider font-sans">Propriétés Publiées</div>
    <div class="mt-4 pt-3 border-t border-slate-100 flex justify-between items-center text-xs text-slate-500">
      <span><?= $stats['properties_sale'] ?> à vendre</span>
      <span class="text-slate-300">•</span>
      <span><?= $stats['properties_rent'] ?> à louer</span>
    </div>
  </a>

  <!-- 2. Contacts Card -->
  <a href="<?= SITE_URL ?>/admin/contacts" class="admin-glass-card rounded-2xl p-6 relative overflow-hidden group hover:shadow-xl transition-all duration-300 transform hover:-translate-y-1">
    <div class="absolute top-0 right-0 w-28 h-28 bg-blue-500/10 rounded-full blur-2xl group-hover:bg-blue-500/20 transition-all"></div>
    <div class="flex items-center justify-between mb-4">
      <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-blue-500 to-indigo-600 text-white flex items-center justify-center shadow-lg shadow-blue-500/30">
        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.75"><path stroke-linecap="round" stroke-linejoin="round" d="M21.75 6.75v10.5a2.25 2.25 0 01-2.25 2.25h-15a2.25 2.25 0 01-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25m19.5 0v.243a2.25 2.25 0 01-1.07 1.916l-7.5 4.615a2.25 2.25 0 01-2.36 0L3.32 8.91a2.25 2.25 0 01-1.07-1.916V6.75"/></svg>
      </div>
      <?php if ($stats['new_contacts'] > 0): ?>
        <span class="text-[10px] uppercase font-bold tracking-widest px-2.5 py-1 bg-blue-500 text-white rounded-full animate-pulse shadow-xs"><?= $stats['new_contacts'] ?> Nouveaux</span>
      <?php else: ?>
        <span class="text-[10px] uppercase font-bold tracking-widest px-2.5 py-1 bg-slate-100 text-slate-600 rounded-full">À jour</span>
      <?php endif; ?>
    </div>
    <div class="text-3xl font-extrabold text-slate-900 mb-1 font-sans"><?= $stats['total_contacts'] ?></div>
    <div class="text-xs font-semibold text-slate-500 uppercase tracking-wider font-sans">Messages Contacts</div>
    <div class="mt-4 pt-3 border-t border-slate-100 flex justify-between items-center text-xs text-slate-500">
      <span>Taux de réponse</span>
      <span class="font-bold text-blue-600">98%</span>
    </div>
  </a>

  <!-- 3. Estimations Card -->
  <a href="<?= SITE_URL ?>/admin/estimations" class="admin-glass-card rounded-2xl p-6 relative overflow-hidden group hover:shadow-xl transition-all duration-300 transform hover:-translate-y-1">
    <div class="absolute top-0 right-0 w-28 h-28 bg-amber-500/10 rounded-full blur-2xl group-hover:bg-amber-500/20 transition-all"></div>
    <div class="flex items-center justify-between mb-4">
      <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-amber-500 to-brand-600 text-white flex items-center justify-center shadow-lg shadow-amber-500/30">
        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.75"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 15.75V18m-7.5-6.75h.008v.008H8.25v-.008zm0 2.25h.008v.008H8.25V13.5zm0 2.25h.008v.008H8.25v-.008zm0 2.25h.008v.008H8.25V18zm2.498-6.75h.007v.008h-.007v-.008zm0 2.25h.007v.008h-.007V13.5zm0 2.25h.007v.008h-.007v-.008zm0 2.25h.007v.008h-.007V18zm2.504-6.75h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V13.5zm0 2.25h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V18zm2.498-6.75h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V13.5zM8.25 6h7.5v2.25h-7.5V6zM12 2.25c-1.892 0-3.758.11-5.593.322C5.307 2.7 4.5 3.616 4.5 4.709v.776M12 2.25c1.892 0 3.758.11 5.593.322 1.1.128 1.907 1.044 1.907 2.137v.776M12 2.25v18.75m0-18.75l-3.75 3.75M12 2.25l3.75 3.75"/></svg>
      </div>
      <?php if ($stats['new_estims'] > 0): ?>
        <span class="text-[10px] uppercase font-bold tracking-widest px-2.5 py-1 bg-amber-500 text-black rounded-full shadow-xs"><?= $stats['new_estims'] ?> À Traiter</span>
      <?php else: ?>
        <span class="text-[10px] uppercase font-bold tracking-widest px-2.5 py-1 bg-slate-100 text-slate-600 rounded-full">Traité</span>
      <?php endif; ?>
    </div>
    <div class="text-3xl font-extrabold text-slate-900 mb-1 font-sans"><?= $stats['total_estims'] ?></div>
    <div class="text-xs font-semibold text-slate-500 uppercase tracking-wider font-sans">Demandes d'Estimation</div>
    <div class="mt-4 pt-3 border-t border-slate-100 flex justify-between items-center text-xs text-slate-500">
      <span>Délai moyen</span>
      <span class="font-bold text-amber-700">&lt; 48 heures</span>
    </div>
  </a>

  <!-- 4. Rendez-vous Card -->
  <a href="<?= SITE_URL ?>/admin/rdv" class="admin-glass-card rounded-2xl p-6 relative overflow-hidden group hover:shadow-xl transition-all duration-300 transform hover:-translate-y-1">
    <div class="absolute top-0 right-0 w-28 h-28 bg-purple-500/10 rounded-full blur-2xl group-hover:bg-purple-500/20 transition-all"></div>
    <div class="flex items-center justify-between mb-4">
      <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-purple-500 to-indigo-700 text-white flex items-center justify-center shadow-lg shadow-purple-500/30">
        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.75"><path stroke-linecap="round" stroke-linejoin="round" d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 9v7.5m-9-6h.008v.008H12V12zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM12 15h.008v.008H12V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM9.75 15h.008v.008H9.75V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM7.5 15h.008v.008H7.5V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM12 18h.008v.008H12V18zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM9.75 18h.008v.008H9.75V18zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM7.5 18h.008v.008H7.5V18zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"/></svg>
      </div>
      <span class="text-[10px] uppercase font-bold tracking-widest px-2.5 py-1 bg-purple-50 text-purple-700 rounded-full border border-purple-200"><?= $stats['rdv_upcoming'] ?> à venir</span>
    </div>
    <div class="text-3xl font-extrabold text-slate-900 mb-1 font-sans"><?= $stats['total_rdvs'] ?></div>
    <div class="text-xs font-semibold text-slate-500 uppercase tracking-wider font-sans">Rendez-vous Honorés</div>
    <div class="mt-4 pt-3 border-t border-slate-100 flex justify-between items-center text-xs text-slate-500">
      <span>Planifiés à Dar Bouazza</span>
      <span class="font-bold text-purple-600">100%</span>
    </div>
  </a>

</div>

<!-- VISITOR TRAFFIC ANALYTICS SECTION -->
<div class="admin-glass-card rounded-2xl p-6 sm:p-8 mb-10 border border-slate-200/80 shadow-xs">
  <div class="flex flex-col sm:flex-row sm:items-center justify-between pb-6 border-b border-slate-100 gap-4 mb-6">
    <div>
      <div class="flex items-center gap-3">
        <div class="w-3 h-3 rounded-full bg-emerald-500 animate-ping"></div>
        <h2 class="text-xl font-bold text-slate-900 font-sans">Statistiques de Fréquentation & Visiteurs</h2>
      </div>
      <p class="text-xs text-slate-500 mt-1">Audience du site web, visites uniques et pages les plus consultées</p>
    </div>
    <div class="flex items-center gap-3">
      <span class="text-xs font-semibold px-3 py-1.5 bg-slate-900 text-white rounded-lg shadow-xs">
        <?= number_format($visitorStats['visitors_today']) ?> Visiteur(s) unique(s) aujourd'hui
      </span>
    </div>
  </div>

  <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
    <div class="bg-gradient-to-br from-slate-900 to-slate-800 text-white p-5 rounded-xl shadow-xs">
      <p class="text-[10px] uppercase font-bold text-slate-400 tracking-wider">Pages Vues (Total)</p>
      <p class="text-2xl font-extrabold font-sans mt-1"><?= number_format($visitorStats['total_views']) ?></p>
      <p class="text-xs text-emerald-400 mt-2 font-medium">↑ En croissance</p>
    </div>

    <div class="bg-gradient-to-br from-indigo-900 to-slate-900 text-white p-5 rounded-xl shadow-xs">
      <p class="text-[10px] uppercase font-bold text-indigo-300 tracking-wider">Visiteurs Uniques</p>
      <p class="text-2xl font-extrabold font-sans mt-1"><?= number_format($visitorStats['unique_visitors']) ?></p>
      <p class="text-xs text-indigo-300 mt-2 font-medium">Adresse(s) IP distinctes</p>
    </div>

    <div class="bg-gradient-to-br from-amber-500 to-brand-600 text-slate-950 p-5 rounded-xl shadow-xs">
      <p class="text-[10px] uppercase font-bold text-slate-900 tracking-wider">Vues Ce Mois-ci</p>
      <p class="text-2xl font-extrabold font-sans mt-1"><?= number_format($visitorStats['views_month']) ?></p>
      <p class="text-xs text-slate-900/80 mt-2 font-medium">Mois en cours</p>
    </div>

    <div class="bg-slate-50 border border-slate-200 p-5 rounded-xl">
      <p class="text-[10px] uppercase font-bold text-slate-500 tracking-wider">Vues Aujourd'hui</p>
      <p class="text-2xl font-extrabold font-sans text-slate-900 mt-1"><?= number_format($visitorStats['views_today']) ?></p>
      <p class="text-xs text-slate-500 mt-2">Dernières 24h</p>
    </div>
  </div>

  <!-- Traffic Chart & Top Pages Grid -->
  <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <!-- 7-Day Traffic Visual Bars -->
    <div class="lg:col-span-2 bg-slate-50 p-6 rounded-xl border border-slate-200/80">
      <h3 class="text-xs font-bold text-slate-900 uppercase tracking-wider mb-4 font-sans flex items-center justify-between">
        <span>Évolution des Visites sur 7 Jours</span>
        <span class="text-[10px] text-slate-400 font-normal">Vues & Visiteurs uniques</span>
      </h3>
      
      <?php 
      $maxViews = 1;
      foreach ($visitorStats['chart_data'] as $cd) {
          if ($cd['views'] > $maxViews) $maxViews = $cd['views'];
      }
      ?>
      <div class="flex items-end justify-between gap-3 h-40 pt-4">
        <?php foreach ($visitorStats['chart_data'] as $cd): 
          $heightPct = max(8, round(($cd['views'] / $maxViews) * 100));
        ?>
          <div class="flex-1 flex flex-col items-center gap-2 group h-full justify-end">
            <div class="text-[10px] font-bold text-slate-600 opacity-0 group-hover:opacity-100 transition-opacity">
              <?= $cd['views'] ?>
            </div>
            <div class="w-full bg-slate-200 rounded-t-md relative overflow-hidden flex items-end" style="height: <?= $heightPct ?>%;">
              <div class="w-full bg-gradient-to-t from-slate-900 to-indigo-600 rounded-t-md transition-all group-hover:bg-amber-500" style="height: 100%;"></div>
            </div>
            <span class="text-[10px] font-medium text-slate-500 font-sans"><?= $cd['date'] ?></span>
          </div>
        <?php endforeach; ?>
      </div>
    </div>

    <!-- Top Visited Pages -->
    <div class="bg-slate-50 p-6 rounded-xl border border-slate-200/80">
      <h3 class="text-xs font-bold text-slate-900 uppercase tracking-wider mb-4 font-sans">
        Pages les Plus Visitées
      </h3>
      <?php if (empty($visitorStats['top_pages'])): ?>
        <p class="text-xs text-slate-400 py-6 text-center font-sans">Aucune donnée de visite disponible.</p>
      <?php else: ?>
        <div class="space-y-3">
          <?php foreach ($visitorStats['top_pages'] as $tp): ?>
            <div class="flex items-center justify-between text-xs py-1.5 border-b border-slate-200/60 last:border-0">
              <span class="font-medium text-slate-800 font-sans truncate max-w-[180px]"><?= htmlspecialchars($tp['page_url']) ?></span>
              <span class="font-bold text-slate-900 bg-white px-2.5 py-1 rounded border border-slate-200 font-sans"><?= $tp['views'] ?> vues</span>
            </div>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    </div>
  </div>
</div>

<!-- SECTION DASHBOARD STATISTICS & PERFORMANCE ANALYSIS -->
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8 mb-10">

  <!-- Left: Analytics Breakdown Cards (2 cols wide) -->
  <div class="lg:col-span-2 admin-glass-card rounded-2xl p-6 sm:p-8">
    <div class="flex items-center justify-between mb-6 pb-4 border-b border-slate-100">
      <div>
        <h2 class="text-lg font-bold text-slate-900 font-sans">Statistiques & Répartition des Demandes</h2>
        <p class="text-xs text-slate-500 mt-0.5">Analyse en temps réel de la provenance des prospects et biens</p>
      </div>
      <span class="text-[10px] tracking-widest uppercase font-bold text-brand-700 bg-brand-50 px-3 py-1.5 rounded-full border border-brand-200">Performances 2026</span>
    </div>

    <!-- Analytics Progress Bars -->
    <div class="space-y-6">
      
      <!-- Metric 1: Demandes de Contacts -->
      <div>
        <div class="flex justify-between items-center mb-2 text-xs font-sans font-semibold">
          <span class="text-slate-700 flex items-center gap-2">
            <span class="w-3 h-3 rounded-full bg-blue-500 inline-block"></span>
            Formulaires de Contact Direct
          </span>
          <span class="text-slate-900 font-bold"><?= $pctContacts ?>% (<?= $stats['total_contacts'] ?> demandes)</span>
        </div>
        <div class="w-full bg-slate-100 rounded-full h-3 overflow-hidden p-0.5 border border-slate-200/50">
          <div class="bg-gradient-to-r from-blue-500 to-indigo-600 h-full rounded-full transition-all duration-1000" style="width: <?= $pctContacts ?>%"></div>
        </div>
      </div>

      <!-- Metric 2: Demandes d'Estimation -->
      <div>
        <div class="flex justify-between items-center mb-2 text-xs font-sans font-semibold">
          <span class="text-slate-700 flex items-center gap-2">
            <span class="w-3 h-3 rounded-full bg-amber-500 inline-block"></span>
            Demandes d'Estimation en Ligne
          </span>
          <span class="text-slate-900 font-bold"><?= $pctEstims ?>% (<?= $stats['total_estims'] ?> demandes)</span>
        </div>
        <div class="w-full bg-slate-100 rounded-full h-3 overflow-hidden p-0.5 border border-slate-200/50">
          <div class="bg-gradient-to-r from-amber-500 to-brand-600 h-full rounded-full transition-all duration-1000" style="width: <?= $pctEstims ?>%"></div>
        </div>
      </div>

      <!-- Metric 3: Rendez-vous Pris -->
      <div>
        <div class="flex justify-between items-center mb-2 text-xs font-sans font-semibold">
          <span class="text-slate-700 flex items-center gap-2">
            <span class="w-3 h-3 rounded-full bg-purple-500 inline-block"></span>
            Prises de Rendez-vous Directes
          </span>
          <span class="text-slate-900 font-bold"><?= $pctRdvs ?>% (<?= $stats['total_rdvs'] ?> demandes)</span>
        </div>
        <div class="w-full bg-slate-100 rounded-full h-3 overflow-hidden p-0.5 border border-slate-200/50">
          <div class="bg-gradient-to-r from-purple-500 to-indigo-700 h-full rounded-full transition-all duration-1000" style="width: <?= $pctRdvs ?>%"></div>
        </div>
      </div>

    </div>

    <!-- Micro Stats Badge Grid -->
    <div class="grid grid-cols-3 gap-4 mt-8 pt-6 border-t border-slate-100">
      <div class="bg-slate-50 p-4 rounded-xl border border-slate-200/60 text-center">
        <p class="text-[10px] text-slate-500 font-bold uppercase tracking-wider mb-1">Ratio Vente/Location</p>
        <p class="text-lg font-extrabold text-slate-900 font-sans"><?= $pctSale ?>% / <?= $pctRent ?>%</p>
      </div>
      <div class="bg-slate-50 p-4 rounded-xl border border-slate-200/60 text-center">
        <p class="text-[10px] text-slate-500 font-bold uppercase tracking-wider mb-1">Satisfaction Client</p>
        <p class="text-lg font-extrabold text-emerald-600 font-sans">99.4%</p>
      </div>
      <div class="bg-slate-50 p-4 rounded-xl border border-slate-200/60 text-center">
        <p class="text-[10px] text-slate-500 font-bold uppercase tracking-wider mb-1">Zone Principale</p>
        <p class="text-lg font-extrabold text-brand-700 font-sans">Dar Bouazza</p>
      </div>
    </div>

  </div>

  <!-- Right: Portfolio Balance Visual -->
  <div class="admin-glass-card rounded-2xl p-6 sm:p-8 flex flex-col justify-between">
    <div>
      <div class="flex items-center justify-between mb-4 pb-3 border-b border-slate-100">
        <h3 class="text-base font-bold text-slate-900 font-sans">Portfolio Immobilière</h3>
        <span class="w-2.5 h-2.5 rounded-full bg-emerald-500"></span>
      </div>
      
      <p class="text-xs text-slate-500 leading-relaxed mb-6 font-sans">
        AJOUAMAI IMMO-BUSINESS concentre ses offres de prestige sur la région de Dar Bouazza et Casablanca littoral.
      </p>

      <div class="space-y-4">
        <div class="p-4 rounded-xl bg-slate-900 text-white flex justify-between items-center">
          <div>
            <p class="text-[10px] uppercase tracking-widest text-brand-500 font-bold">À Vendre</p>
            <p class="text-xl font-bold font-sans mt-0.5"><?= $stats['properties_sale'] ?> Propriétés</p>
          </div>
          <span class="text-2xl font-serif text-brand-500"><?= $pctSale ?>%</span>
        </div>

        <div class="p-4 rounded-xl bg-slate-800 text-white flex justify-between items-center">
          <div>
            <p class="text-[10px] uppercase tracking-widest text-purple-400 font-bold">À Louer</p>
            <p class="text-xl font-bold font-sans mt-0.5"><?= $stats['properties_rent'] ?> Propriétés</p>
          </div>
          <span class="text-2xl font-serif text-purple-400"><?= $pctRent ?>%</span>
        </div>
      </div>
    </div>

    <a href="<?= SITE_URL ?>/admin/properties" class="mt-6 block text-center py-3 bg-brand-600 hover:bg-brand-700 text-white text-xs font-bold uppercase tracking-widest rounded-xl transition-all shadow-md shadow-brand-600/30">
      Gérer le catalogue complet →
    </a>
  </div>

</div>

<!-- 3-COLUMN LEADS DATA TABLES -->
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">

  <!-- 1. Recent Contacts -->
  <div class="admin-glass-card rounded-2xl overflow-hidden flex flex-col">
    <div class="p-6 border-b border-slate-100 flex justify-between items-center bg-slate-50/50">
      <div>
        <h3 class="text-base font-bold text-slate-900 font-sans">Messages Récents</h3>
        <p class="text-[11px] text-slate-500 font-sans">Dernières demandes envoyées</p>
      </div>
      <a href="<?= SITE_URL ?>/admin/contacts" class="text-xs font-bold text-brand-700 hover:underline">Voir tout →</a>
    </div>

    <div class="divide-y divide-slate-100 flex-1">
      <?php if (empty($recentContacts)): ?>
        <p class="p-6 text-xs text-slate-400 text-center font-sans">Aucun message pour le moment.</p>
      <?php else: ?>
        <?php foreach ($recentContacts as $c): ?>
        <div class="p-4 hover:bg-slate-50 transition-colors">
          <div class="flex items-start justify-between gap-2">
            <div>
              <p class="text-xs font-bold text-slate-900 font-sans"><?= htmlspecialchars($c['nom']) ?></p>
              <p class="text-[11px] text-slate-500 font-sans"><?= htmlspecialchars($c['email']) ?></p>
            </div>
            <span class="text-[9px] uppercase font-bold tracking-wider px-2 py-0.5 rounded-md <?= $c['status'] === 'nouveau' ? 'bg-blue-500 text-white' : 'bg-slate-100 text-slate-600' ?>">
              <?= $c['status'] ?>
            </span>
          </div>
          <p class="text-[10px] text-slate-400 mt-2 font-sans flex items-center justify-between">
            <span>Profil: <?= htmlspecialchars($c['profil_je_suis'] ?? '—') ?></span>
            <span><?= date('d/m H:i', strtotime($c['created_at'])) ?></span>
          </p>
        </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </div>

  <!-- 2. Recent Estimations -->
  <div class="admin-glass-card rounded-2xl overflow-hidden flex flex-col">
    <div class="p-6 border-b border-slate-100 flex justify-between items-center bg-slate-50/50">
      <div>
        <h3 class="text-base font-bold text-slate-900 font-sans">Dernières Estimations</h3>
        <p class="text-[11px] text-slate-500 font-sans">Évaluations déposées</p>
      </div>
      <a href="<?= SITE_URL ?>/admin/estimations" class="text-xs font-bold text-brand-700 hover:underline">Voir tout →</a>
    </div>

    <div class="divide-y divide-slate-100 flex-1">
      <?php if (empty($recentEstims)): ?>
        <p class="p-6 text-xs text-slate-400 text-center font-sans">Aucune estimation enregistrée.</p>
      <?php else: ?>
        <?php foreach ($recentEstims as $e): ?>
        <div class="p-4 hover:bg-slate-50 transition-colors">
          <div class="flex items-start justify-between gap-2">
            <div>
              <p class="text-xs font-bold text-slate-900 font-sans"><?= htmlspecialchars($e['nom']) ?></p>
              <p class="text-[11px] text-slate-500 font-sans capitalize">Bien: <?= htmlspecialchars($e['type_bien'] ?? '—') ?></p>
            </div>
            <span class="text-[9px] uppercase font-bold tracking-wider px-2 py-0.5 rounded-md <?= $e['status'] === 'nouveau' ? 'bg-amber-500 text-black font-bold' : 'bg-slate-100 text-slate-600' ?>">
              <?= $e['status'] ?>
            </span>
          </div>
          <p class="text-[10px] text-slate-400 mt-2 font-sans flex items-center justify-between">
            <span><?= htmlspecialchars($e['email']) ?></span>
            <span><?= date('d/m H:i', strtotime($e['created_at'])) ?></span>
          </p>
        </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </div>

  <!-- 3. Upcoming RDVs -->
  <div class="admin-glass-card rounded-2xl overflow-hidden flex flex-col">
    <div class="p-6 border-b border-slate-100 flex justify-between items-center bg-slate-50/50">
      <div>
        <h3 class="text-base font-bold text-slate-900 font-sans">Rendez-vous à Venir</h3>
        <p class="text-[11px] text-slate-500 font-sans">Agenda des rendez-vous</p>
      </div>
      <a href="<?= SITE_URL ?>/admin/rdv" class="text-xs font-bold text-brand-700 hover:underline">Voir tout →</a>
    </div>

    <div class="divide-y divide-slate-100 flex-1">
      <?php if (empty($nextRdvs)): ?>
        <p class="p-6 text-xs text-slate-400 text-center font-sans">Aucun rendez-vous à venir.</p>
      <?php else: ?>
        <?php foreach ($nextRdvs as $r): ?>
        <div class="p-4 hover:bg-slate-50 transition-colors">
          <div class="flex items-start justify-between gap-2">
            <div>
              <p class="text-xs font-bold text-slate-900 font-sans"><?= htmlspecialchars($r['prenom'] . ' ' . $r['nom']) ?></p>
              <p class="text-[11px] text-purple-700 font-bold font-sans mt-0.5">
                📅 <?= date('d/m/Y', strtotime($r['date_rdv'])) ?> à <?= htmlspecialchars(substr($r['heure'],0,5)) ?>
              </p>
            </div>
            <span class="text-[9px] uppercase font-bold tracking-wider px-2 py-0.5 rounded-md bg-purple-100 text-purple-800">
              <?= $r['status'] ?>
            </span>
          </div>
        </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>
  </div>

</div>

<?php include __DIR__ . '/layout_footer.php'; ?>
