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

$pdo = getDB();
$projects       = $pdo->query("SELECT * FROM projects ORDER BY created_at DESC")->fetchAll();
$totalProjects  = count($projects);
$totalInvitees  = $pdo->query("SELECT COUNT(*) FROM invitees")->fetchColumn();
$totalAccepted  = $pdo->query("SELECT COUNT(*) FROM invitees WHERE response_status='Accept'")->fetchColumn();
$totalDeclined  = $pdo->query("SELECT COUNT(*) FROM invitees WHERE response_status='Decline'")->fetchColumn();
$totalPending   = $totalInvitees - $totalAccepted - $totalDeclined;
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Super Admin — Dashboard</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">

      <!-- Stats -->
      <div class="stats-grid">
        <div class="stat-card"><div class="num"><?= $totalProjects ?></div><div class="lbl">Projects</div></div>
        <div class="stat-card"><div class="num"><?= $totalInvitees ?></div><div class="lbl">Total Invitees</div></div>
        <div class="stat-card"><div class="num"><?= $totalAccepted ?></div><div class="lbl">Accepted</div></div>
        <div class="stat-card"><div class="num"><?= $totalDeclined ?></div><div class="lbl">Declined</div></div>
        <div class="stat-card"><div class="num"><?= $totalPending ?></div><div class="lbl">Pending</div></div>
      </div>

      <!-- Projects -->
      <div class="card">
        <div class="card-header">
          <span class="card-title">All Wedding Projects</span>
          <a href="create-project.php" class="btn btn-primary btn-sm">+ New Project</a>
        </div>
        <?php if (empty($projects)): ?>
          <p style="color:var(--text-muted);text-align:center;padding:40px 0;">No projects yet. <a href="create-project.php" style="color:var(--gold);">Create your first one →</a></p>
        <?php else: ?>
        <div class="projects-grid">
          <?php foreach ($projects as $p):
            $cnt = $pdo->prepare("SELECT COUNT(*) FROM invitees iv JOIN invitations i ON iv.invitation_id=i.id WHERE i.project_id=?");
            $cnt->execute([$p['id']]);
            $invCount = $cnt->fetchColumn();
          ?>
          <div class="project-card">
            <h3><?= htmlspecialchars($p['groom_name']) ?> &amp; <?= htmlspecialchars($p['bride_name']) ?></h3>
            <div class="sub">
              📁 <?= htmlspecialchars($p['directory_name']) ?><br/>
              📅 <?= date('M j, Y', strtotime($p['wedding_datetime'])) ?><br/>
              👥 <?= $invCount ?> invitees
            </div>
            <div class="actions">
              <a href="edit-project.php?id=<?= $p['id'] ?>" class="btn btn-outline btn-sm">Edit</a>
              <a href="manage-invitees.php?id=<?= $p['id'] ?>" class="btn btn-outline btn-sm">Invitees</a>
              <a href="rsvp-report.php?id=<?= $p['id'] ?>" class="btn btn-outline btn-sm">RSVP</a>
              <a href="<?= PROJECTS_URL ?>/<?= urlencode($p['directory_name']) ?>/" class="btn btn-outline btn-sm" target="_blank">View ↗</a>
            </div>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
      </div>

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