<?php
echo "<h2>সিস্টেম চেক টেস্ট</h2>";

// Test basic PHP functionality
echo "<p>PHP Version: " . phpversion() . "</p>";
echo "<p>Basic PHP is working: ✓</p>";

// Test database connection using the actual config
echo "<h3>Testing Database Connection</h3>";
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_TIMEOUT => 30
    ]);

    echo "<p>Database connection: ✓</p>";
    
    // Read settings directly
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE '%sms%' OR setting_key LIKE '%non_musking%'");
    $stmt->execute();
    $settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h4>Found Settings:</h4><ul>";
    foreach ($settings as $setting) {
        $key = htmlspecialchars($setting['setting_key']);
        $value = $setting['setting_key'] === 'non_musking_password' || $setting['setting_key'] === 'sms_password' ? '***HIDDEN***' : htmlspecialchars($setting['setting_value']);
        echo "<li>$key = $value</li>";
    }
    echo "</ul>";
    
} catch (Exception $e) {
    echo "<p>Database connection failed: ✗ (" . $e->getMessage() . ")</p>";
    echo "<p>Please check your database configuration in config/database.php</p>";
}

echo "<h3>Testing cURL Availability</h3>";
if (function_exists('curl_version')) {
    $curl_version = curl_version();
    echo "<p>cURL is available: ✓ (Version: {$curl_version['version']})</p>";
} else {
    echo "<p>cURL is available: ✗ (cURL extension not loaded)</p>";
}

echo "<h3>PHP Extensions</h3>";
$extensions = ['pdo', 'curl', 'mysqli', 'mbstring', 'json'];
echo "<ul>";
foreach ($extensions as $ext) {
    $status = extension_loaded($ext) ? '✓' : '✗';
    echo "<li>$ext: $status</li>";
}
echo "</ul>";

echo "<h3>Testing API Endpoint Reachability</h3>";
// Test the MRAM endpoint directly
$test_url = 'https://rest.mrmessaging.net/messages';
if (function_exists('curl_init')) {
    $ch = curl_init($test_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_NOBODY, true);  // HEAD request
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlError = curl_error($ch);
    curl_close($ch);
    
    if ($curlError) {
        echo "<p>API Endpoint Test: ✗ (Error: " . htmlspecialchars($curlError) . ")</p>";
    } else {
        echo "<p>API Endpoint Test: ✓ (HTTP Code: $httpCode)</p>";
    }
} else {
    echo "<p>Cannot test API endpoint - cURL not available</p>";
}

echo "<p><strong>Next Steps:</strong> If this page loads, try the sync script: <a href='sync_with_mram.php'>Sync with MRAM</a></p>";
?>