<?php
// --- START: ADDED DATABASE CONNECTION CODE ---
// This code is self-contained so it does not conflict with your config.php file.
$servername = "localhost"; // Usually correct for cPanel
$username = "gobolcha_gobolchain"; // <-- IMPORTANT: REPLACE THIS
$password = "@Jibala1997"; // <-- IMPORTANT: REPLACE THIS
$dbname = "gobolcha_gobolchain"; // <-- IMPORTANT: REPLACE THIS

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Set character set
$conn->set_charset("utf8mb4");
// --- END: ADDED DATABASE CONNECTION CODE ---


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = $_POST['email'];
    $token = $_POST['token'];

    // 1. Check if the token exists and is valid
    $stmt = $conn->prepare("SELECT * FROM password_resets WHERE email = ? AND token = ? AND expires_at >= NOW()");
    $stmt->bind_param("ss", $email, $token);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        // 2. Token is valid. Redirect the user to the reset password page with the token.
        // This token will be used to authorize the password reset.
        header("Location: reset-password.html?token=$token&email=$email");
        exit;
    } else {
        // 3. Invalid or expired token.
        echo "The verification code is invalid or has expired.";
    }

    $stmt->close();
    $conn->close();
}
?>