<?php
header('Content-Type: text/plain');
echo "PING OK\n";
echo "PHP version: " . phpversion() . "\n";

@session_start();
echo "session user_id   = " . ($_SESSION['user_id'] ?? '(none)') . "\n";
echo "session company_id= " . ($_SESSION['company_id'] ?? '(none)') . "\n";
echo "session role      = " . ($_SESSION['role'] ?? '(none)') . "\n";

echo "database.php exists  = " . (file_exists(__DIR__ . '/../config/database.php') ? 'yes' : 'NO') . "\n";
echo "sso_config.php exists= " . (file_exists(__DIR__ . '/../config/sso_config.php') ? 'yes' : 'NO') . "\n";

// Try loading sso_config in isolation and report any error
echo "--- trying to load sso_config.php ---\n";
try {
    require __DIR__ . '/../config/sso_config.php';
    echo "sso_config loaded OK\n";
    echo "SSO_SHARED_SECRET defined = " . (defined('SSO_SHARED_SECRET') ? 'yes (' . strlen(SSO_SHARED_SECRET) . ' chars)' : 'NO') . "\n";
    echo "link function exists      = " . (function_exists('sso_service_portal_link') ? 'yes' : 'NO') . "\n";
} catch (Throwable $e) {
    echo "ERROR loading sso_config: " . $e->getMessage() . "\n";
}

echo "--- trying to load database.php ---\n";
try {
    require __DIR__ . '/../config/database.php';
    echo "database.php loaded OK, \$pdo is " . (isset($pdo) ? 'set' : 'NOT set') . "\n";
} catch (Throwable $e) {
    echo "ERROR loading database.php: " . $e->getMessage() . "\n";
}

echo "DONE\n";