<?php
session_start();
if (!isset($_SESSION['admin_id'])) { header('Location: login.php'); exit; }
include '../db.php';  // <-- this line makes $pdo available

// Add these queries:
$totalCompanies = $pdo->query("SELECT COUNT(*) FROM companies")->fetchColumn();
$totalUsers = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
$totalReviews = $pdo->query("SELECT COUNT(*) FROM reviews")->fetchColumn();
$topCompany = $pdo->query("
  SELECT c.name, ROUND(AVG(r.rating),1) AS avg_rating
  FROM companies c JOIN reviews r ON c.id=r.company_id
  GROUP BY c.id ORDER BY avg_rating DESC LIMIT 1
")->fetch();
?>

<?php include 'menu.php'; ?>

<!DOCTYPE html>
<html>
<head>
    <title>Dashboard</title>
    <link rel="stylesheet" href="../style.css">
</head>
<body>

<h2>Dashboard Stats</h2>
<ul>
  <li>Total Companies: <?= $totalCompanies ?></li>
  <li>Total Users: <?= $totalUsers ?></li>
  <li>Total Reviews: <?= $totalReviews ?></li>
  <?php if($topCompany): ?>
    <li>Top Company: <?= htmlspecialchars($topCompany['name']) ?> (<?= $topCompany['avg_rating'] ?>/5)</li>
  <?php endif; ?>
</ul>
</body>
</html>