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

require_login();

$min_coverage = (int)($_GET['min'] ?? 0);
$max_coverage = (int)($_GET['max'] ?? 100);

// GP Coverage with filtering
$stmt = $pdo->prepare("
  SELECT 
    d.name AS district_name,
    b.name AS block_name,
    g.id, g.name AS gp_name,
    COUNT(DISTINCT s.id) AS total_schools,
    COUNT(DISTINCT v.school_id) AS visited_schools,
    COALESCE(ROUND(100 * COUNT(DISTINCT v.school_id) / NULLIF(COUNT(DISTINCT s.id), 0), 1), 0) AS coverage_pct,
    COUNT(DISTINCT v.id) AS total_visits,
    MAX(v.visit_date) AS last_visit
  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
  HAVING coverage_pct >= ? AND coverage_pct <= ?
  ORDER BY coverage_pct DESC, d.name, b.name
");
$stmt->execute([$min_coverage, $max_coverage]);
$gps = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "GP Coverage Analysis";
require_once __DIR__ . "/../public/partials/header.php";
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h5 class="mb-0">GP Coverage Analysis</h5>
    <div class="text-muted small">Coverage percentage by GP</div>
  </div>
</div>

<div class="card card-soft shadow-soft p-3 mb-3">
  <form class="row g-2" method="get">
    <div class="col-md-3">
      <label class="form-label small">Min Coverage %</label>
      <input type="number" class="form-control form-control-sm" name="min" min="0" max="100" value="<?= htmlspecialchars((string)$min_coverage) ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label small">Max Coverage %</label>
      <input type="number" class="form-control form-control-sm" name="max" min="0" max="100" value="<?= htmlspecialchars((string)$max_coverage) ?>">
    </div>
    <div class="col-md-6 d-flex align-items-end gap-2">
      <button type="submit" class="btn btn-outline-dark btn-sm">Filter</button>
      <a href="?" class="btn btn-outline-secondary btn-sm">Reset</a>
    </div>
  </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>District</th>
          <th>Block</th>
          <th>GP</th>
          <th>Schools</th>
          <th>Visited</th>
          <th>Coverage %</th>
          <th>Visits</th>
          <th>Last Visit</th>
        </tr>
      </thead>
      <tbody>
        <?php if (!$gps): ?>
          <tr><td colspan="8" class="text-muted text-center py-3">No GPs found with coverage between <?= $min_coverage ?>%-<?= $max_coverage ?>%.</td></tr>
        <?php endif; ?>

        <?php foreach ($gps as $gp): 
          $coverage = (float)($gp['coverage_pct'] ?? 0);
          $coverage_class = $coverage >= 80 ? 'bg-success' : ($coverage >= 50 ? 'bg-warning' : 'bg-danger');
        ?>
        <tr>
          <td><?= htmlspecialchars($gp['district_name'] ?? 'N/A') ?></td>
          <td><?= htmlspecialchars($gp['block_name'] ?? 'N/A') ?></td>
          <td class="fw-semibold"><?= htmlspecialchars($gp['gp_name']) ?></td>
          <td><?= (int)$gp['total_schools'] ?></td>
          <td class="fw-semibold"><?= (int)$gp['visited_schools'] ?></td>
          <td>
            <div class="d-flex align-items-center gap-2">
              <div class="progress" style="min-width: 60px; height: 0.5rem;">
                <div class="progress-bar <?= $coverage_class ?>" style="width: <?= min($coverage, 100) ?>%"></div>
              </div>
              <span class="badge <?= $coverage_class ?>"><?= $coverage ?>%</span>
            </div>
          </td>
          <td><?= (int)$gp['total_visits'] ?></td>
          <td class="text-nowrap text-muted small"><?= $gp['last_visit'] ? htmlspecialchars($gp['last_visit']) : '—' ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

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