<?php
require_once __DIR__ . '/lib.php';

$token  = $_GET['token'] ?? '';
$status = 'invalid';

if ($token) {
    $pdo  = db();
    $stmt = $pdo->prepare('SELECT id, email_verified_at FROM bk_users WHERE reset_token = ? AND reset_token_expires > NOW()');
    $stmt->execute([$token]);
    $user = $stmt->fetch();

    if ($user) {
        if ($user['email_verified_at']) {
            $status = 'already';
        } else {
            $pdo->prepare('UPDATE bk_users SET email_verified_at = NOW(), reset_token = NULL, reset_token_expires = NULL WHERE id = ?')
                ->execute([$user['id']]);
            $status = 'ok';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>Email Verification — <?= esc(APP_NAME) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com"/>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin/>
<link href="https://fonts.googleapis.com/css2?family=Syne:wght@700;800&family=DM+Sans:opsz,wght@9..40,400;9..40,600&display=swap" rel="stylesheet"/>
<style>
*{box-sizing:border-box;margin:0;padding:0}
:root{--bg:#f4f5f7;--surface:#fff;--border:#e8eaed;--accent:#e09500;--accent-hi:#f5a623;--accent-d:#b87800;--accent-glow:rgba(224,149,0,.18);--green:#16a34a;--green-pale:#f0fdf4;--green-border:#bbf7d0;--amber:#d97706;--amber-pale:#fffbeb;--amber-border:#fde68a;--red:#dc2626;--red-pale:#fef2f2;--red-border:#fecaca;--text:#111827;--text-2:#4b5563;--text-3:#9ca3af;--font-d:'Syne',sans-serif;--font-b:'DM Sans',sans-serif}
html,body{min-height:100vh;background:var(--bg);color:var(--text);font-family:var(--font-b);-webkit-font-smoothing:antialiased;font-size:14px;line-height:1.6;display:flex;align-items:center;justify-content:center;padding:24px 16px}
a{text-decoration:none;color:inherit}
.card{width:100%;max-width:400px;background:var(--surface);border-radius:20px;box-shadow:0 2px 8px rgba(0,0,0,.05),0 12px 40px rgba(0,0,0,.08);border:1px solid var(--border);overflow:hidden;text-align:center}
.card-hdr{background:linear-gradient(135deg,var(--accent-hi),var(--accent-d));padding:28px}
.card-hdr-name{font-family:var(--font-d);font-size:15px;font-weight:800;color:rgba(255,255,255,.8);letter-spacing:.02em}
.card-hdr-title{font-family:var(--font-d);font-size:22px;font-weight:800;color:#fff;margin-top:4px}
.card-body{padding:36px 28px}
.icon{width:68px;height:68px;border-radius:50%;margin:0 auto 18px;display:flex;align-items:center;justify-content:center;font-size:30px}
.icon-ok{background:var(--green-pale);border:2px solid var(--green-border)}
.icon-warn{background:var(--amber-pale);border:2px solid var(--amber-border)}
.icon-err{background:var(--red-pale);border:2px solid var(--red-border)}
.status-title{font-family:var(--font-d);font-size:18px;font-weight:800;color:var(--text);margin-bottom:8px}
.status-msg{font-size:14px;color:var(--text-2);margin-bottom:28px;line-height:1.6}
.btn{display:inline-block;padding:12px 28px;background:var(--accent);color:#fff;font-family:var(--font-b);font-size:14px;font-weight:700;border-radius:10px;transition:background .15s,box-shadow .15s}
.btn:hover{background:var(--accent-hi);box-shadow:0 4px 12px var(--accent-glow)}
.btn-outline{background:transparent;color:var(--text-2);border:1.5px solid var(--border);margin-top:10px;display:inline-block;padding:10px 24px;border-radius:10px;font-size:13px;font-weight:600}
</style>
</head>
<body>
<div class="card">
  <div class="card-hdr">
    <div class="card-hdr-name"><?= esc(APP_NAME) ?></div>
    <div class="card-hdr-title">Email Verification</div>
  </div>
  <div class="card-body">
    <?php if ($status === 'ok'): ?>
      <div class="icon icon-ok">✅</div>
      <div class="status-title">Email verified!</div>
      <p class="status-msg">Your account is confirmed and ready to go. Sign in to start booking.</p>
      <a href="login.php" class="btn">Sign In Now</a>

    <?php elseif ($status === 'already'): ?>
      <div class="icon icon-warn">☑️</div>
      <div class="status-title">Already verified</div>
      <p class="status-msg">This email address has already been verified. Go ahead and sign in.</p>
      <a href="login.php" class="btn">Sign In</a>

    <?php else: ?>
      <div class="icon icon-err">❌</div>
      <div class="status-title">Link expired or invalid</div>
      <p class="status-msg">This verification link is no longer valid. It may have expired (24 hours) or already been used.</p>
      <a href="register.php" class="btn">Register Again</a>
      <br/>
      <a href="login.php" class="btn-outline">Back to Sign In</a>
    <?php endif; ?>
  </div>
</div>
</body>
</html>
