<?php
echo "<h2>বিস্তারিত ডায়াগনস্টিক টেস্ট</h2>";

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

// Test file includes
echo "<h3>Testing File Inclusions</h3>";
$config_path = '../config/database.php';
$helper_path = '../includes/sms_helper.php';

if (file_exists($config_path)) {
    echo "<p>Database config exists: ✓</p>";
} else {
    echo "<p>Database config exists: ✗ (File not found: $config_path)</p>";
}

if (file_exists($helper_path)) {
    echo "<p>SMS helper exists: ✓</p>";
} else {
    echo "<p>SMS helper exists: ✗ (File not found: $helper_path)</p>";
}

echo "<h3>Testing Manual Configuration</h3>";
echo "<p>Attempting to read settings directly from database...</p>";

try {
    // Direct database connection test
    $db_config = parse_ini_file('../config/config.ini');
    $host = $db_config['host'] ?? 'localhost';
    $dbname = $db_config['dbname'] ?? 'bidda';
    $username = $db_config['username'] ?? 'root';
    $password = $db_config['password'] ?? '';
    
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    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.</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 "<p><strong>Note:</strong> If this page loads, try accessing other diagnostic pages. If it doesn't load, there may be a server configuration issue.</p>";
?>