<?php
// superadmin/rsvp-report.php
session_start();
require_once '../includes/config.php';
require_once '../includes/sa_auth.php';
require_once '../includes/db.php';
requireSuperAdmin();

$pdo = getDB();
$id  = intval($_GET['id'] ?? 0);
$proj = $pdo->prepare("SELECT * FROM projects WHERE id=?");
$proj->execute([$id]);
$p = $proj->fetch();
if (!$p) { header('Location: index.php'); exit; }

$rows = $pdo->prepare("
  SELECT i.token, iv.id, iv.name, iv.response_status, iv.responded_at
  FROM invitations i JOIN invitees iv ON i.id=iv.invitation_id
  WHERE i.project_id=? ORDER BY i.created_at DESC, iv.id ASC
");
$rows->execute([$id]);
$all = $rows->fetchAll();

$grouped  = [];
foreach ($all as $r) $grouped[$r['token']][] = $r;

$total    = count($all);
$accepted = count(array_filter($all, fn($r)=>$r['response_status']==='Accept'));
$declined = count(array_filter($all, fn($r)=>$r['response_status']==='Decline'));
$pending  = $total - $accepted - $declined;
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>RSVP Report</title>
  <link rel="stylesheet" href="sa.css"/>
</head>
<body>
<div class="sa-wrapper">
  <?php include 'sidebar.php'; ?>
  <div class="sa-main">
    <?php include 'topbar.php'; ?>
    <div class="sa-content">

      <div class="alert alert-info" style="margin-bottom:24px;">
        <strong><?= htmlspecialchars($p['groom_name']) ?> &amp; <?= htmlspecialchars($p['bride_name']) ?></strong>
        &nbsp;|&nbsp; <?= date('F j, Y', strtotime($p['wedding_datetime'])) ?>
        &nbsp;|&nbsp; <a href="<?= PROJECTS_URL ?>/<?= urlencode($p['directory_name']) ?>/" target="_blank" style="color:var(--gold-light)">View Site ↗</a>
      </div>

      <div class="stats-grid" style="margin-bottom:24px;">
        <div class="stat-card"><div class="num"><?= $total ?></div><div class="lbl">Total Invitees</div></div>
        <div class="stat-card"><div class="num" style="color:#4caf50"><?= $accepted ?></div><div class="lbl">Accepted</div></div>
        <div class="stat-card"><div class="num" style="color:#e53935"><?= $declined ?></div><div class="lbl">Declined</div></div>
        <div class="stat-card"><div class="num" style="color:#888"><?= $pending ?></div><div class="lbl">Pending</div></div>
      </div>

      <div class="card">
        <div class="card-header">
          <span class="card-title">RSVP Responses by Group</span>
          <button class="btn btn-outline btn-sm" onclick="location.reload()">↻ Refresh</button>
        </div>

        <?php if (empty($grouped)): ?>
          <p style="text-align:center;color:var(--text-muted);padding:40px 0;">No invitees added yet.</p>
        <?php else: foreach ($grouped as $token => $invitees): ?>
          <div style="background:var(--dark4);border-radius:8px;padding:16px;margin-bottom:16px;">
            <div style="font-size:13px;color:var(--text-muted);margin-bottom:10px;">
              <strong style="color:var(--gold)">Token:</strong> <?= htmlspecialchars($token) ?>
              &nbsp;|&nbsp;
              <code style="font-size:11px;"><?= PROJECTS_URL ?>/<?= urlencode($p['directory_name']) ?>/index.html?token=<?= urlencode($token) ?></code>
            </div>
            <table class="sa-table">
              <tr><th>Name</th><th>Status</th><th>Responded At</th></tr>
              <?php foreach ($invitees as $iv): ?>
              <tr>
                <td><?= htmlspecialchars($iv['name']) ?></td>
                <td>
                  <?php if ($iv['response_status'] === 'Accept'): ?>
                    <span class="badge-status badge-accept">✓ Accepted</span>
                  <?php elseif ($iv['response_status'] === 'Decline'): ?>
                    <span class="badge-status badge-decline">✗ Declined</span>
                  <?php else: ?>
                    <span class="badge-status badge-pending">Pending</span>
                  <?php endif; ?>
                </td>
                <td style="color:var(--text-muted);font-size:13px;"><?= $iv['responded_at'] ?? '—' ?></td>
              </tr>
              <?php endforeach; ?>
            </table>
          </div>
        <?php endforeach; endif; ?>
      </div>

      <a href="index.php" class="btn btn-outline">← Back to Dashboard</a>

    </div>
  </div>
</div>
</body>
</html>
