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

echo "<h2>Config Diagnostic</h2>";

// Check if config file exists
echo "<h3>1. Config File Check</h3>";
if (file_exists('config.php')) {
    echo "<p style='color: green;'>✅ Config file found</p>";
    
    // Try to include and see what happens
    try {
        include_once 'config.php';
        echo "<p style='color: green;'>✅ Config included successfully</p>";
    } catch (Exception $e) {
        echo "<p style='color: red;'>❌ Config include failed: " . $e->getMessage() . "</p>";
    }
    
    // Check what variables are defined after config
    echo "<h3>2. Variable Check</h3>";
    $global_vars = get_defined_vars();
    echo "<p>Variables after config include:</p><ul>";
    foreach ($global_vars as $var => $value) {
        if (!in_array($var, ['GLOBALS', '_POST', '_GET', '_COOKIE', '_SESSION', '_FILES', '_ENV', 'php_errormsg', 'http_response_header'])) {
            if (is_scalar($value) || is_array($value)) {
                $display_value = is_array($value) ? 'Array(' . count($value) . ')' : $value;
                echo "<li><strong>$var:</strong> " . htmlspecialchars($display_value) . "</li>";
            } else {
                echo "<li><strong>$var:</strong> " . gettype($value) . "</li>";
            }
        }
    }
    echo "</ul>";
    
} else {
    echo "<p style='color: red;'>❌ Config file not found</p>";
    echo "<p>Current directory: " . getcwd() . "</p>";
    echo "<p>Files in current directory:</p><ul>";
    $files = scandir('.');
    foreach ($files as $file) {
        if ($file != '.' && $file != '..') {
            echo "<li>$file</li>";
        }
    }
    echo "</ul>";
}

// Test database connection manually
echo "<h3>3. Manual Database Connection Test</h3>";

// Common XAMPP database settings
$host = 'localhost';
$username = 'root';  // Default XAMPP username
$password = '';      // Default XAMPP password (usually empty)
$database = 'divine_reports';  // Adjust if needed

echo "<p>Testing manual connection with:</p>";
echo "<ul>";
echo "<li>Host: $host</li>";
echo "<li>Username: $username</li>";
echo "<li>Password: " . (empty($password) ? '(empty)' : $password) . "</li>";
echo "<li>Database: $database</li>";
echo "</ul>";

try {
    $test_connection = new mysqli($host, $username, $password, $database);
    
    if ($test_connection->connect_error) {
        echo "<p style='color: red;'>❌ Manual connection failed: " . $test_connection->connect_error . "</p>";
    } else {
        echo "<p style='color: green;'>✅ Manual connection successful!</p>";
        
        // Test a simple query
        $result = $test_connection->query("SELECT COUNT(*) as count FROM schools");
        if ($result) {
            $row = $result->fetch_assoc();
            echo "<p style='color: green;'>✅ Query successful - Found " . $row['count'] . " schools</p>";
        } else {
            echo "<p style='color: orange;'>⚠️ Query failed - " . $test_connection->error . "</p>";
        }
        
        $test_connection->close();
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Manual connection exception: " . $e->getMessage() . "</p>";
}

echo "<h3>4. Recommendations</h3>";
echo "<ul>";
echo "<li>Check if your config.php defines the correct database credentials</li>";
echo "<li>Verify that your XAMPP MySQL server is running</li>";
echo "<li>Ensure the database 'divine_reports' exists</li>";
echo "<li>Check if your config.php variable name matches what the report system expects</li>";
echo "</ul>";
?>