<?php
require_once '../config.php';
require_once '../functions.php';
requireAdmin();

$ratings = $pdo->query("SELECT rat.*, u.name as user_name, c.name as car_name, ch.name as chauffeur_name,
                                r.reservation_date
                         FROM ratings rat
                         JOIN reservations r ON rat.reservation_id=r.id
                         JOIN users u ON rat.user_id=u.id
                         JOIN cars c ON r.car_id=c.id
                         JOIN chauffeurs ch ON r.chauffeur_id=ch.id
                         ORDER BY rat.created_at DESC")->fetchAll();

$avg = $pdo->query("SELECT ROUND(AVG(stars),2) FROM ratings")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Avis clients – Admin First Car</title>
    <link rel="stylesheet" href="../style.css">
    <style>
      .admin-layout{display:grid;grid-template-columns:220px 1fr;min-height:100vh;}
      .admin-sidebar{background:rgba(0,0,0,.6);border-right:1px solid rgba(232,130,12,.15);padding:2rem 1.2rem;position:sticky;top:0;height:100vh;}
      .admin-sidebar .nav-logo{font-size:1.4rem;display:block;margin-bottom:.3rem;}
      .admin-sidebar .admin-badge{font-size:.65rem;text-transform:uppercase;letter-spacing:.12em;color:var(--orange);font-weight:700;margin-bottom:2rem;display:block;}
      .sidebar-nav{list-style:none;}.sidebar-nav li{margin-bottom:.3rem;}
      .sidebar-nav a{display:block;padding:.65rem 1rem;border-radius:8px;color:var(--light2);text-decoration:none;font-size:.82rem;font-weight:500;transition:all .2s;}
      .sidebar-nav a:hover,.sidebar-nav a.active{background:rgba(232,130,12,.12);color:var(--orange);}
      .admin-main{padding:2rem 2.5rem;}
    </style>
</head>
<body>
<div class="admin-layout">
  <aside class="admin-sidebar">
    <a href="../index.php" class="nav-logo">First Car<span>.</span></a>
    <span class="admin-badge">Administration</span>
    <ul class="sidebar-nav">
      <li><a href="index.php">📊 Tableau de bord</a></li>
      <li><a href="reservations.php">📅 Réservations</a></li>
      <li><a href="users.php">👥 Utilisateurs</a></li>
      <li><a href="chauffeurs.php">🎩 Chauffeurs</a></li>
      <li><a href="cars.php">🚗 Véhicules</a></li>
      <li><a href="ratings.php" class="active">⭐ Avis clients</a></li>
      <li style="margin-top:2rem;border-top:1px solid rgba(255,255,255,.06);padding-top:1rem;">
        <a href="../dashboard.php">← Espace client</a></li>
      <li><a href="../logout.php" style="color:#f44336;">Déconnexion</a></li>
    </ul>
  </aside>
  <main class="admin-main">
    <h1 style="font-family:'Cormorant Garamond',serif;font-size:2rem;margin-bottom:.5rem;">Avis clients</h1>
    <?php if ($avg): ?>
    <p style="color:var(--light2);margin-bottom:1.5rem;">
      Note moyenne : <span style="color:var(--orange);font-size:1.3rem;font-weight:700;"><?= $avg ?>/5 ⭐</span>
      <span style="opacity:.6;">(<?= count($ratings) ?> avis)</span>
    </p>
    <?php endif; ?>

    <div class="reservations-table-wrapper">
      <table class="reservations-table">
        <thead>
          <tr><th>Client</th><th>Véhicule</th><th>Chauffeur</th><th>Date trajet</th><th>Note</th><th>Commentaire</th></tr>
        </thead>
        <tbody>
          <?php foreach ($ratings as $rat): ?>
          <tr>
            <td><?= sanitize($rat['user_name']) ?></td>
            <td><?= sanitize($rat['car_name']) ?></td>
            <td><?= sanitize($rat['chauffeur_name']) ?></td>
            <td><?= date('d/m/Y', strtotime($rat['reservation_date'])) ?></td>
            <td>
              <span style="color:var(--orange);"><?= str_repeat('★',$rat['stars']) ?></span><span style="opacity:.25;"><?= str_repeat('★',5-$rat['stars']) ?></span>
            </td>
            <td style="font-size:.82rem;color:var(--light2);max-width:250px;">
              <?= sanitize($rat['comment'] ?: '—') ?>
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </main>
</div>
</body>
</html>