<?php
session_start();
require_once 'config/database.php';

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

if (empty($token)) {
    $_SESSION['login_error'] = 'Invalid verification link.';
    header('Location: index.php');
    exit;
}

try {
    $stmt = $pdo->prepare("UPDATE users SET email_verified = 1, verification_token = NULL WHERE verification_token = ?");
    $stmt->execute([$token]);

    if ($stmt->rowCount() > 0) {
        $_SESSION['login_error'] = 'Email verified successfully! You can now login.';
    } else {
        $_SESSION['login_error'] = 'Invalid or expired verification link.';
    }
} catch (PDOException $e) {
    error_log("Verification error: " . $e->getMessage());
    $_SESSION['login_error'] = 'An error occurred. Please try again.';
}

header('Location: index.php');
?>