<?php
include "../connect/db.php";

if (isset($_GET['source'])) {
    $token = $_GET['source'];

    // Query to find the token in the database
    $sql = "SELECT * FROM email_confirmation WHERE token = ?";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('s', $token);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        // Token found, update the email confirmation status
        $update_sql = "UPDATE email_confirmation SET confirmed = 1, confirmed_at = NOW() WHERE token = ?";
        $update_stmt = $mysqli->prepare($update_sql);
        $update_stmt->bind_param('s', $token);
        $update_stmt->execute();

        echo "
        <script>alert('Email confirmed successfully!')
                       setTimeout(function(){ window.location ='../index.html'} , 500);

        </script>
        ";
    } else {
        
        echo 
       "<script>alert('Invalid or expired confirmation link.')
               setTimeout(function(){ window.location ='../index.html'} , 500);

       </script>"  

        ;
    }
}

?>