<?php
/**
 * Email Verification Handler
 * PHP User Registration - https://rskworld.in
 * Developer: Molla Samser | RSK World
 * Contact: help@rskworld.in | +91 93305 39277
 * Year: 2026
 */

require_once __DIR__ . '/includes/init.php';
require_once __DIR__ . '/includes/functions.php';

$token = trim($_GET['token'] ?? '');
$message = '';
$isError = true;

if ($token !== '') {
    $pdo = getDbConnection();
    $stmt = $pdo->prepare('SELECT id, name, token_expires_at FROM users WHERE verification_token = ? AND email_verified = 0');
    $stmt->execute([$token]);
    $user = $stmt->fetch();
    if ($user) {
        if (strtotime($user['token_expires_at']) >= time()) {
            $stmt = $pdo->prepare('UPDATE users SET email_verified = 1, verification_token = NULL, token_expires_at = NULL WHERE id = ?');
            $stmt->execute([$user['id']]);
            $message = 'Your email has been verified. You can now log in.';
            $isError = false;
        } else {
            $message = 'This verification link has expired. Please request a new one or register again.';
        }
    } else {
        $message = 'Invalid or already used verification link.';
    }
} else {
    $message = 'No verification token provided.';
}

$pageTitle = 'Email Verification';
require_once __DIR__ . '/includes/header.php';
?>

<div class="auth-card verify-card">
    <h1><i class="fas fa-envelope-open-text"></i> Email Verification</h1>
    <p class="subtitle">PHP User Registration — <a href="https://rskworld.in" target="_blank">rskworld.in</a></p>
    <div class="alert <?= $isError ? 'alert-error' : 'alert-success' ?>"><?= htmlspecialchars($message) ?></div>
    <p><a href="login.php" class="btn btn-primary">Go to Login</a></p>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
