<?php
declare(strict_types=1);
if (!isset($pdo)) {
    $pdo = require_once __DIR__ . '/config/database.php';
}
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/header.php';

$token = sanitize_str($_GET['token'] ?? '');
$msg = 'Invalid or expired token.';
if ($token) {
  $q = $pdo->prepare("SELECT * FROM email_verifications WHERE token=? LIMIT 1");
  $q->execute([$token]); $row = $q->fetch();
  if ($row && empty($row['verified_at']) && strtotime((string)$row['expires_at']) > time()) {
    $pdo->prepare("UPDATE users SET email_verified_at=? WHERE id=?")->execute([date('c'), (int)$row['user_id']]);
    $pdo->prepare("UPDATE email_verifications SET verified_at=? WHERE id=?")->execute([date('c'), (int)$row['id']]);
    $msg = 'Your email has been verified successfully.';
  }
}
?>
<section class="section" style="padding-top:24px">
  <div class="card"><h3>Email Verification</h3><p class="lead" style="margin:0"><?=$msg?></p></div>
</section>
<?php include __DIR__ . '/includes/footer.php'; ?>
