<?php
declare(strict_types=1);
require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../config/auth.php";

require_login();

$limit = (int)($_GET['limit'] ?? 50);
if ($limit < 10 || $limit > 500) $limit = 50;

// Top GPs by unique visited schools
$stmt = $pdo->prepare("
  SELECT 
    g.id, g.name, b.name AS block_name, d.name AS district_name,
    COUNT(DISTINCT v.school_id) AS visited_schools,
    COUNT(DISTINCT s.id) AS total_schools,
    ROUND(100 * COUNT(DISTINCT v.school_id) / COUNT(DISTINCT s.id), 1) AS coverage_pct,
    COUNT(DISTINCT v.id) AS total_visits,
    COUNT(DISTINCT v.visited_by) AS active_reps
  FROM gps g
  LEFT JOIN blocks b ON b.id = g.block_id
  LEFT JOIN districts d ON d.id = b.district_id
  LEFT JOIN schools s ON s.gp_id = g.id
  LEFT JOIN visits v ON v.school_id = s.id
  GROUP BY g.id
  ORDER BY visited_schools DESC
  LIMIT ?
");
$stmt->execute([$limit]);
$gps = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Top GPs";
require_once __DIR__ . "/../public/partials/header.php";
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h5 class="mb-0">Top GPs Report</h5>
    <div class="text-muted small">GPs ranked by unique schools visited</div>
  </div>
  <form method="get" class="d-flex gap-2">
    <select class="form-select form-select-sm" name="limit" onchange="this.form.submit()">
      <option value="10" <?= $limit === 10 ? 'selected' : '' ?>>Top 10</option>
      <option value="25" <?= $limit === 25 ? 'selected' : '' ?>>Top 25</option>
      <option value="50" <?= $limit === 50 ? 'selected' : '' ?>>Top 50</option>
      <option value="100" <?= $limit === 100 ? 'selected' : '' ?>>Top 100</option>
    </select>
  </form>
</div>

<div class="card card-soft shadow-soft p-3">
  <div class="table-responsive">
    <table class="table table-hover align-middle mb-0 small">
      <thead>
        <tr>
          <th>#</th>
          <th>District</th>
          <th>Block</th>
          <th>GP</th>
          <th>Schools Visited</th>
          <th>Coverage %</th>
          <th>Total Visits</th>
          <th>Active Reps</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($gps as $idx => $g): ?>
        <tr>
          <td class="fw-semibold"><?= $idx + 1 ?></td>
          <td><?= htmlspecialchars($g['district_name'] ?? 'N/A') ?></td>
          <td><?= htmlspecialchars($g['block_name'] ?? 'N/A') ?></td>
          <td class="fw-semibold"><?= htmlspecialchars($g['name']) ?></td>
          <td><?= (int)$g['visited_schools'] ?>/<?= (int)$g['total_schools'] ?></td>
          <td>
            <span class="badge <?= $g['coverage_pct'] >= 80 ? 'bg-success' : ($g['coverage_pct'] >= 50 ? 'bg-warning' : 'bg-danger') ?>">
              <?= (float)$g['coverage_pct'] ?>%
            </span>
          </td>
          <td><?= (int)$g['total_visits'] ?></td>
          <td><?= (int)$g['active_reps'] ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

<?php require_once __DIR__ . "/../public/partials/footer.php"; ?>