<?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;

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

$daily = $pdo->prepare("
  SELECT visit_date AS d, COUNT(*) AS c, COUNT(DISTINCT school_id) AS schools, COUNT(DISTINCT visited_by) AS reps
  FROM visits
  WHERE visit_date >= ?
  GROUP BY visit_date
  ORDER BY visit_date ASC
")->execute([$from_date]);

$stmt = $pdo->prepare("
  SELECT visit_date AS d, COUNT(*) AS c, COUNT(DISTINCT school_id) AS schools, COUNT(DISTINCT visited_by) AS reps
  FROM visits
  WHERE visit_date >= ?
  GROUP BY visit_date
  ORDER BY visit_date ASC
");
$stmt->execute([$from_date]);
$daily_data = $stmt->fetchAll(PDO::FETCH_ASSOC);

$total_visits = array_sum(array_column($daily_data, 'c'));
$total_schools = count(array_unique(array_column($daily_data, 'schools')));
$avg_per_day = count($daily_data) > 0 ? round($total_visits / count($daily_data), 1) : 0;

$page_title = "Daily Report";
require_once __DIR__ . "/../public/partials/header.php";
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h5 class="mb-0">Daily Visit 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="row g-3 mb-3">
  <div class="col-md-3">
    <div class="card card-soft shadow-soft p-3">
      <div class="text-muted small">Total Visits</div>
      <div class="h4 mb-0"><?= $total_visits ?></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card card-soft shadow-soft p-3">
      <div class="text-muted small">Unique Schools</div>
      <div class="h4 mb-0"><?= $total_schools ?></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card card-soft shadow-soft p-3">
      <div class="text-muted small">Avg Per Day</div>
      <div class="h4 mb-0"><?= $avg_per_day ?></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card card-soft shadow-soft p-3">
      <div class="text-muted small">Days with Visits</div>
      <div class="h4 mb-0"><?= count($daily_data) ?></div>
    </div>
  </div>
</div>

<div class="card card-soft shadow-soft p-3">
  <h6 class="mb-3">Visit Trend</h6>
  <div class="table-responsive">
    <table class="table table-sm align-middle mb-0">
      <thead>
        <tr>
          <th>Date</th>
          <th>Visits</th>
          <th>Unique Schools</th>
          <th>Active Reps</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($daily_data as $d): ?>
        <tr>
          <td class="fw-semibold"><?= htmlspecialchars($d['d']) ?></td>
          <td><?= (int)$d['c'] ?></td>
          <td><?= (int)$d['schools'] ?></td>
          <td><?= (int)$d['reps'] ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>

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