<?php
/**
 * Simple JWT token test
 */

// Test token provided by user
$test_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoib3BlbmNvZGUiLCJuYW1lIjoiT3BlbkNvZGUiLCJBUElfVElNRSI6MTc2MTczNDQ4Nn0.vjukCjNwBCElzP7iT_eWEHhxzL5KPDZ7e05DR1OZEbE';

echo "Testing JWT token: " . substr($test_token, 0, 50) . "...\n\n";

try {
    // Include JWT library
    require_once 'third_party/node.php';
    require_once 'vendor/autoload.php';

    // Use the JWT key from config
    $config = require_once 'config/jwt.php';
    $jwt_key = $config['jwt_key'];

    echo "Using JWT key: " . substr($jwt_key, 0, 20) . "...\n";

    // Decode the token
    $decoded = \Firebase\JWT\JWT::decode($test_token, new \Firebase\JWT\Key($jwt_key, 'HS256'));

    echo "\n✅ SUCCESS: Token decoded successfully!\n";
    echo "Token payload:\n";
    echo "- User: " . ($decoded->user ?? 'N/A') . "\n";
    echo "- Name: " . ($decoded->name ?? 'N/A') . "\n";
    echo "- API_TIME: " . ($decoded->API_TIME ?? 'N/A') . "\n";

    // Check if token is expired
    if (isset($decoded->API_TIME)) {
        $token_time = $decoded->API_TIME;
        $current_time = time();

        if ($token_time < $current_time) {
            echo "⚠️  WARNING: Token appears to be expired (API_TIME: $token_time < current: $current_time)\n";
        } else {
            echo "✅ Token is still valid (expires: " . date('Y-m-d H:i:s', $token_time) . ")\n";
        }
    }

} catch (Exception $e) {
    echo "\n❌ ERROR: Token validation failed: " . $e->getMessage() . "\n";
    echo "This could mean:\n";
    echo "- Invalid token format\n";
    echo "- Wrong JWT secret key\n";
    echo "- Token has expired\n";
    echo "- Token was tampered with\n";
}
?>