<div class="sidebar-card">
  <h4>Latest Reviews</h4>
  <?php foreach($latestReviews as $rev): ?>
    <p><strong><?= htmlspecialchars($rev['username']) ?></strong> on <em><?= htmlspecialchars($rev['company_name']) ?></em>:</p>
    <div>
      <?php for($i=1;$i<=5;$i++): ?>
        <span class="star-box <?= $i<=$rev['rating'] ? 'filled' : '' ?>">★</span>
      <?php endfor; ?>
      (<?= $rev['rating'] ?>/5)
    </div>
    <p style="margin-top:2px;"><?= htmlspecialchars(substr($rev['comment'],0,50)) ?>...</p>
    <hr>
  <?php endforeach; ?>
</div>


<div class="sidebar-card">
  <h4>Top Rated Companies</h4>
  <?php
  $topCompanies = $pdo->query("
    SELECT c.id, c.name, c.image, ROUND(AVG(r.rating),1) AS avg_rating
    FROM companies c
    JOIN reviews r ON c.id = r.company_id
    GROUP BY c.id
    HAVING COUNT(r.id) >= 1
    ORDER BY avg_rating DESC
    LIMIT 5
  ")->fetchAll();
  foreach($topCompanies as $top): ?>
    <div style="display:flex;align-items:center;gap:8px;margin-bottom:5px;">
      <img src="images/companies/<?= htmlspecialchars($top['image']) ?>" alt="<?= htmlspecialchars($top['name']) ?>" style="width:30px;height:30px;object-fit:contain;">
      <span><?= htmlspecialchars($top['name']) ?> (<?= $top['avg_rating'] ?>/5)</span>
    </div>
  <?php endforeach; ?>
</div>
