<?php
/**
 * Database Connection Test
 * Upload this to your server to test connection
 */

// Show all errors
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Database Connection Test</h2>";

// Test 1: Direct connection
echo "<h3>Test 1: Direct Connection</h3>";
try {
    $pdo = new PDO("mysql:host=localhost;dbname=zashofco_ashof;charset=utf8mb4", "zashofco_ashof", "%wfJGwv[hnP3Wcct", [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
    echo "<p style='color: green;'>✓ Direct connection successful!</p>";
    
    // Test query
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM users");
    $result = $stmt->fetch();
    echo "<p>Total users: " . $result['count'] . "</p>";
    
} catch(PDOException $e) {
    echo "<p style='color: red;'>✗ Direct connection failed: " . $e->getMessage() . "</p>";
}

// Test 2: EnvLoader test
echo "<h3>Test 2: EnvLoader Test</h3>";
if (file_exists(__DIR__ . '/EnvLoader.php')) {
    require_once __DIR__ . '/EnvLoader.php';
    
    $db_host = EnvLoader::get('DB_HOST', 'localhost');
    $db_name = EnvLoader::get('DB_NAME', '');
    $db_user = EnvLoader::get('DB_USER', '');
    $db_pass = EnvLoader::get('DB_PASS', '');
    
    echo "<p>DB_HOST: " . ($db_host ? $db_host : 'NOT SET') . "</p>";
    echo "<p>DB_NAME: " . ($db_name ? $db_name : 'NOT SET') . "</p>";
    echo "<p>DB_USER: " . ($db_user ? $db_user : 'NOT SET') . "</p>";
    echo "<p>DB_PASS: " . ($db_pass ? '***SET***' : 'NOT SET') . "</p>";
    
    // Test with EnvLoader values
    if ($db_name && $db_user) {
        try {
            $pdo2 = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8mb4", $db_user, $db_pass, [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            ]);
            echo "<p style='color: green;'>✓ EnvLoader connection successful!</p>";
        } catch(PDOException $e) {
            echo "<p style='color: red;'>✗ EnvLoader connection failed: " . $e->getMessage() . "</p>";
        }
    }
} else {
    echo "<p style='color: orange;'>⚠ EnvLoader.php not found</p>";
}

// Test 3: Check if .env file exists
echo "<h3>Test 3: Environment File Check</h3>";
if (file_exists(__DIR__ . '/.env')) {
    echo "<p style='color: green;'>✓ .env file exists</p>";
    $content = file_get_contents(__DIR__ . '/.env');
    if (strpos($content, 'zashofco_ashof') !== false) {
        echo "<p style='color: green;'>✓ Correct database credentials found in .env</p>";
    } else {
        echo "<p style='color: red;'>✗ Wrong credentials in .env file</p>";
    }
} else {
    echo "<p style='color: red;'>✗ .env file NOT FOUND</p>";
}

// Test 4: SMS System Check
echo "<h3>Test 4: SMS System Check</h3>";
if (file_exists(__DIR__ . '/../config/database.php')) {
    echo "<p>Database config found at: /config/database.php</p>";
    
    // Try to include and see if SMS system causes the error
    try {
        // Don't actually include, just check syntax
        $content = file_get_contents(__DIR__ . '/../config/database.php');
        if (strpos($content, 'SMS') !== false || strpos($content, 'sms') !== false) {
            echo "<p style='color: orange;'>⚠ SMS references found in database.php</p>";
        }
    } catch(Exception $e) {
        echo "<p>Error reading file: " . $e->getMessage() . "</p>";
    }
} else {
    echo "<p style='color: orange;'>⚠ database.php not in expected location</p>";
}

echo "<hr>";
echo "<p><strong>Next Steps:</strong></p>";
echo "<ol>";
echo "<li>If 'Direct Connection' fails, check MySQL user permissions</li>";
echo "<li>If '.env file NOT FOUND', upload the .env file</li>";
echo "<li>If 'Wrong credentials in .env', update the .env file</li>";
echo "<li>Clear browser cache after fixing</li>";
echo "</ol>";
