<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "Step 1: Before includes\n";

require_once __DIR__ . '/../../includes/config.php';
echo "Step 2: After config.php\n";

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
echo "Step 3: Session started\n";

// Skip auth check for test
// require_once __DIR__ . '/../../includes/superadmin_auth.php';

$pageTitle = 'Configurações';
echo "Step 4: Before header\n";

// Test DB connection
try {
    $stmt = $pdo->query("SELECT 1");
    echo "Step 5: DB OK\n";
} catch (Exception $e) {
    echo "Step 5: DB Error: " . $e->getMessage() . "\n";
}

// Test getSetting function
function getSetting($pdo, $key, $default = '') {
    $stmt = $pdo->prepare("SELECT setting_value FROM saas_settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result ? $result['setting_value'] : $default;
}

echo "Step 6: Function defined\n";

$test = getSetting($pdo, 'saas_name', 'default');
echo "Step 7: getSetting works, value: " . $test . "\n";

echo "\n\nAll steps passed!";
