<?php
require_once __DIR__ . '/../src/bootstrap.php';

if (!($_ENV['APP_DEBUG'] ?? false)) {
    header('HTTP/1.0 404 Not Found');
    exit('Debug page only available in development mode');
}

echo "<h2>Debug - Keycloak Configuration</h2>";
echo "<pre>";

echo "Environment Variables:\n";
echo "KEYCLOAK_URL: " . ($_ENV['KEYCLOAK_URL'] ?? 'Not set') . "\n";
echo "KEYCLOAK_REALM: " . ($_ENV['KEYCLOAK_REALM'] ?? 'Not set') . "\n";
echo "KEYCLOAK_CLIENT_ID: " . ($_ENV['KEYCLOAK_CLIENT_ID'] ?? 'Not set') . "\n";
echo "APP_URL: " . ($_ENV['APP_URL'] ?? 'Not set') . "\n\n";

$keycloakConfig = require __DIR__ . '/../config/keycloak.php';
echo "Keycloak Config:\n";
print_r($keycloakConfig);

echo "\nSession Data:\n";
print_r($_SESSION);

echo "\nRequest Data:\n";
echo "REQUEST_URI: " . $_SERVER['REQUEST_URI'] . "\n";
echo "HTTP_HOST: " . $_SERVER['HTTP_HOST'] . "\n";
echo "HTTPS: " . (isset($_SERVER['HTTPS']) ? 'Yes' : 'No') . "\n";

echo "</pre>";

// Test Keycloak connectivity
echo "<h3>Testing Keycloak Connectivity</h3>";
$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $_ENV['KEYCLOAK_URL'] ?? '',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_FOLLOWLOCATION => true
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);

echo "<pre>";
echo "HTTP Code: $httpCode\n";
if ($error) {
    echo "cURL Error: $error\n";
} else {
    echo "Connection successful\n";
}
echo "</pre>";
?>