<?php
// Set error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Define root directory
$rootDir = dirname(__DIR__);

// Include required files
require_once $rootDir . '/vendor/autoload.php';
require_once $rootDir . '/connection.php';

use Firebase\JWT\JWT;

// Your JWT secret key (should match the one used to sign the token)
$jwtSecret = '1ED495A77B65435D317B3E6582E4C'; // Replace with your actual secret key

// The token to decode
$token = '2a19232fadba918d657ec689f2346c881841452f14e6f84e4ee26d42963a947f';

try {
    // Decode the token
    $decoded = JWT::decode($token, new Firebase\JWT\Key($jwtSecret, 'HS256'));
    
    // The decoded token is an object, convert to array for better readability
    $decodedArray = (array) $decoded;
    
    echo "<h2>Decoded Token:</h2>";
    echo "<pre>";
    print_r($decodedArray);
    echo "</pre>";
    
} catch (Exception $e) {
    echo "Error decoding token: " . $e->getMessage();
    
    // If it's not a JWT format, check if it's a simple hash
    if (strpos($e->getMessage(), 'Wrong number of segments') !== false) {
        echo "<p>This appears to be a hash, not a JWT token.</p>";
        echo "<p>Hash algorithm might be: " . (strlen($token) === 64 ? 'SHA-256' : 'Unknown') . "</p>";
    }
}

// Add a form to test other tokens
echo '
<h2>Test Another Token</h2>
<form method="post">
    <textarea name="token" style="width: 100%; height: 100px;">'.htmlspecialchars($token).'</textarea><br>
    <input type="submit" value="Decode Token">
</form>';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['token'])) {
    $token = $_POST['token'];
    // Redirect to same page with token as query parameter
    header("Location: ?token=" . urlencode($token));
    exit;
}
?>
