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

require_login();

$days = (int)($_GET['days'] ?? 30);
if ($days < 1 || $days > 365) $days = 30;

$from_date = date('Y-m-d', strtotime("-{$days} days"));

// Rep Performance
$stmt = $pdo->prepare("
  SELECT 
    u.id, u.name, u.email,
    COUNT(*) AS total_visits,
    COUNT(DISTINCT v.school_id) AS unique_schools,
    COUNT(DISTINCT v.visit_date) AS active_days,
    COUNT(CASE WHEN v.permission_status='APPROVED' THEN 1 END) AS approved,
    COUNT(CASE WHEN v.permission_status='PENDING' THEN 1 END) AS pending,
    COUNT(CASE WHEN v.permission_status='REJECTED' THEN 1 END) AS rejected,
    COUNT(CASE WHEN v.followup_required=1 THEN 1 END) AS followups_created,
    MAX(v.visit_date) AS last_visit
  FROM users u
  LEFT JOIN visits v ON v.visited_by = u.id AND v.visit_date >= ?
  WHERE u.role='REP' AND u.active=1
  GROUP BY u.id
  ORDER BY total_visits DESC
");
$stmt->execute([$from_date]);
$reps = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Rep Performance";
require_once __DIR__ . "/../public/partials/header.php";
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h5 class="mb-0">Rep Performance Report</h5>
    <div class="text-muted small">Last <?= $days ?> days</div>
  </div>
  <form class="d-flex gap-2 align-items-center" method="get">
    <select class="form-select form-select-sm" name="days" onchange="this.form.submit()">
      <option value="7" <?= $days === 7 ? 'selected' : '' ?>>Last 7 days</option>
      <option value="30" <?= $days === 30 ? 'selected' : '' ?>>Last 30 days</option>
      <option value="90" <?= $days === 90 ? 'selected' : '' ?>>Last 90 days</option>
      <option value="365" <?= $days === 365 ? 'selected' : '' ?>>Last year</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>Rep Name</th>
          <th>Email</th>
          <th>Visits</th>
          <th>Schools</th>
          <th>Active Days</th>
          <th>Approved</th>
          <th>Pending</th>
          <th>Rejected</th>
          <th>Follow-ups</th>
          <th>Last Visit</th>
        </tr>
      </thead>
      <tbody>
        <?php if (!$reps): ?>
          <tr><td colspan="10" class="text-muted text-center py-3">No data.</td></tr>
        <?php endif; ?>

        <?php foreach ($reps as $r): ?>
        <tr>
          <td class="fw-semibold"><?= htmlspecialchars($r['name']) ?></td>
          <td class="text-muted small"><?= htmlspecialchars($r['email']) ?></td>
          <td class="fw-semibold"><?= (int)$r['total_visits'] ?></td>
          <td><?= (int)$r['unique_schools'] ?></td>
          <td><?= (int)$r['active_days'] ?></td>
          <td><span class="badge bg-success"><?= (int)$r['approved'] ?></span></td>
          <td><span class="badge bg-warning text-dark"><?= (int)$r['pending'] ?></span></td>
          <td><span class="badge bg-danger"><?= (int)$r['rejected'] ?></span></td>
          <td><span class="badge bg-info"><?= (int)$r['followups_created'] ?></span></td>
          <td class="text-nowrap"><?= $r['last_visit'] ? htmlspecialchars($r['last_visit']) : '—' ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

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