<?php
/**
 * Email Verification Page
 * Digital Product Store
 */

require_once __DIR__ . '/config/database.php';

$token = isset($_GET['token']) ? sanitize($_GET['token']) : '';

if (empty($token)) {
    set_flash_message('Invalid verification link.', 'danger');
    header('Location: login.php');
    exit;
}

// Find user with this token
$stmt = $pdo->prepare("SELECT * FROM users WHERE email_verification_token = ? AND email_verified = 0");
$stmt->execute([$token]);
$user = $stmt->fetch();

if (!$user) {
    set_flash_message('Invalid or expired verification link.', 'danger');
    header('Location: login.php');
    exit;
}

// Verify email
$stmt = $pdo->prepare("UPDATE users SET email_verified = 1, email_verification_token = NULL WHERE id = ?");
$stmt->execute([$user['id']]);

set_flash_message('Your email has been verified! You can now log in.', 'success');
header('Location: login.php');
exit;
?>
