<?php
// event/confirmed.php — Guest ticket confirmation page
// Shown when a guest taps/scans their own QR code
// Does NOT check them in — only confirms their ticket is valid
require_once __DIR__ . '/../config.php';

$token = trim($_GET['token'] ?? '');
if (!$token) { include __DIR__ . '/not_found.php'; exit; }

$db   = db();
$stmt = $db->prepare(
    'SELECT g.*, e.title event_title, e.event_date, e.venue, e.slug, e.description, e.timezone
     FROM guests g JOIN events e ON e.id=g.event_id WHERE g.token=?'
);
$stmt->execute([$token]);
$guest = $stmt->fetch();

if (!$guest) { include __DIR__ . '/not_found.php'; exit; }

$ticketUrl = !empty($guest['invite_path'])
    ? BASE_URL . '/' . ltrim($guest['invite_path'], '/')
    : null;

$eventDate = $guest['event_date'] ? fmtEventTime($guest['event_date'], $guest, 'l, F j, Y') : null;
$eventTime = $guest['event_date'] ? fmtEventTime($guest['event_date'], $guest, 'g:i A')     : null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Your Ticket — <?= htmlspecialchars($guest['event_title']) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" media="print" onload="this.media='all'" href="https://fonts.googleapis.com/css2?family=Syne:wght@400;600;700;800&family=DM+Sans:wght@300;400;500&display=swap">
<link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/main.css">
<style>
  body { background: var(--obsidian); }
  .confirm-wrap {
    min-height: 100vh;
    display: flex; align-items: center; justify-content: center;
    padding: 40px 20px;
    background:
      radial-gradient(ellipse at 20% 30%, rgba(197,158,89,.1) 0%, transparent 55%),
      radial-gradient(ellipse at 80% 70%, rgba(197,158,89,.06) 0%, transparent 55%),
      var(--obsidian);
  }
  .confirm-card {
    max-width: 480px; width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
  }
  .confirm-header {
    background: linear-gradient(135deg, rgba(197,158,89,.15) 0%, transparent 60%);
    border-bottom: 1px solid var(--border);
    padding: 28px 28px 24px;
    text-align: center;
  }
  .confirm-valid {
    display: inline-flex; align-items: center; gap: 8px;
    background: rgba(62,207,142,.12);
    border: 1px solid rgba(62,207,142,.3);
    border-radius: 100px;
    padding: 6px 16px;
    font-family: var(--font-ui); font-size: .68rem;
    font-weight: 700; letter-spacing: .12em; text-transform: uppercase;
    color: var(--success); margin-bottom: 20px;
  }
  .confirm-valid::before {
    content: ''; width: 7px; height: 7px;
    border-radius: 50%; background: var(--success);
    animation: pulse 1.8s ease-in-out infinite;
  }
  @keyframes pulse {
    0%,100% { opacity:1; transform:scale(1); }
    50%      { opacity:.4; transform:scale(.8); }
  }
  .guest-name {
    font-family: var(--font-display);
    font-size: clamp(1.5rem, 4vw, 2.2rem);
    line-height: 1.1; margin-bottom: 10px;
  }
  .confirm-body { padding: 24px 28px; }
  .info-row {
    display: flex; gap: 10px;
    padding: 12px 0;
    border-bottom: 1px solid rgba(255,255,255,.05);
  }
  .info-row:last-child { border-bottom: none; }
  .info-icon { font-size: 1rem; flex-shrink: 0; width: 24px; text-align: center; margin-top: 1px; }
  .info-label { font-family: var(--font-ui); font-size: .65rem; font-weight: 700;
                letter-spacing: .1em; text-transform: uppercase; color: var(--muted); margin-bottom: 2px; }
  .info-value { font-size: .9rem; color: var(--white); }
  .confirm-footer {
    padding: 20px 28px 28px;
    border-top: 1px solid var(--border);
    text-align: center;
  }
  .ticket-img {
    width: 100%; border-radius: var(--radius);
    margin-bottom: 16px; display: block;
    border: 1px solid var(--border);
  }
  .notice {
    background: rgba(197,158,89,.08);
    border: 1px solid rgba(197,158,89,.2);
    border-radius: var(--radius);
    padding: 12px 16px;
    font-size: .8rem; color: var(--muted);
    text-align: left; margin-top: 16px;
    display: flex; gap: 10px; align-items: flex-start;
  }
  .powered {
    margin-top: 40px; text-align: center;
    color: var(--muted); font-size: .7rem;
    font-family: var(--font-ui); letter-spacing: .1em; text-transform: uppercase;
  }
</style>
</head>
<body>
<div class="confirm-wrap">
  <div style="width:100%;max-width:480px">
    <div style="text-align:center;margin-bottom:24px">
      <div style="width:40px;height:3px;background:var(--gold);margin:0 auto 0"></div>
    </div>

    <div class="confirm-card">
      <!-- Header -->
      <div class="confirm-header">
        <div class="confirm-valid">✓ Valid Ticket</div>
        <div class="guest-name"><?= htmlspecialchars($guest['name']) ?></div>
        <div style="color:var(--gold);font-family:var(--font-display);font-size:1rem;opacity:.85">
          <?= htmlspecialchars($guest['event_title']) ?>
        </div>
        <?php if ($guest['category']): ?>
          <span class="badge badge-gold" style="margin-top:10px"><?= htmlspecialchars($guest['category']) ?></span>
        <?php endif; ?>
        <?php if ($guest['table_number']): ?>
          <span class="badge badge-muted" style="margin-top:10px">
            Table <?= htmlspecialchars($guest['table_number']) ?>
            <?= $guest['seat_number'] ? ' · Seat ' . htmlspecialchars($guest['seat_number']) : '' ?>
          </span>
        <?php endif; ?>
      </div>

      <!-- Event details -->
      <div class="confirm-body">
        <?php if ($eventDate): ?>
        <div class="info-row">
          <div class="info-icon">📅</div>
          <div>
            <div class="info-label">Date & Time</div>
            <div class="info-value"><?= $eventDate ?> · <?= $eventTime ?></div>
          </div>
        </div>
        <?php endif; ?>
        <?php if ($guest['venue']): ?>
        <div class="info-row">
          <div class="info-icon">📍</div>
          <div>
            <div class="info-label">Venue</div>
            <div class="info-value"><?= htmlspecialchars($guest['venue']) ?></div>
          </div>
        </div>
        <?php endif; ?>
        <?php if ($guest['email']): ?>
        <div class="info-row">
          <div class="info-icon">✉</div>
          <div>
            <div class="info-label">Email</div>
            <div class="info-value"><?= htmlspecialchars($guest['email']) ?></div>
          </div>
        </div>
        <?php endif; ?>

        <!-- Check-in status notice -->
        <?php if ($guest['checked_in']): ?>
          <div class="notice" style="border-color:rgba(62,207,142,.3);background:rgba(62,207,142,.06)">
            <span>✅</span>
            <div>
              <strong style="color:var(--success)">Already checked in</strong><br>
              <?= date('g:i A · M j', strtotime($guest['check_in_time'])) ?>
            </div>
          </div>
        <?php else: ?>
          <div class="notice">
            <span>ℹ️</span>
            <div>
              <strong style="color:var(--gold)">Not yet checked in</strong><br>
              Please present this ticket to the check-in desk on arrival.
            </div>
          </div>
        <?php endif; ?>
      </div>

      <!-- Ticket image + download -->
      <?php if ($ticketUrl): ?>
      <div class="confirm-footer">
        <img src="<?= htmlspecialchars($ticketUrl) ?>" alt="Your ticket" class="ticket-img">
        <a href="<?= htmlspecialchars($ticketUrl) ?>" download class="btn btn-gold" style="width:100%;justify-content:center">
          ↓ Save Ticket to Phone
        </a>
        <p class="text-muted text-sm" style="margin-top:10px">
          Screenshot this or download to show at the entrance.
        </p>
      </div>
      <?php else: ?>
      <div class="confirm-footer">
        <div class="text-muted text-sm">Your ticket is being prepared. Check back closer to the event.</div>
      </div>
      <?php endif; ?>
    </div>

    <div class="powered">Powered by RSVP System</div>
  </div>
</div>
</body>
</html>
