<?php
require_once 'config/database.php';
require_once 'includes/sms_helper.php';

echo "<h2>Non-Masking SMS Setup Diagnosis</h2>";

// Check database tables
echo "<h3>1. Database Tables Check</h3>";
try {
    // Check if settings table exists
    $stmt = $pdo->query("SHOW TABLES LIKE 'settings'");
    $settingsTable = $stmt->fetch();
    
    if ($settingsTable) {
        echo "✓ Settings table exists<br>";
        
        // Check if messages_log table exists
        $stmt = $pdo->query("SHOW TABLES LIKE 'messages_log'");
        $messagesTable = $stmt->fetch();
        
        if ($messagesTable) {
            echo "✓ Messages log table exists<br>";
        } else {
            echo "✗ Messages log table does not exist<br>";
        }
    } else {
        echo "✗ Settings table does not exist<br>";
    }
} catch (Exception $e) {
    echo "✗ Database error: " . $e->getMessage() . "<br>";
}

// Check current configuration
echo "<h3>2. Current Configuration</h3>";
$config = non_musking_config();

echo "Enabled: " . ($config['enabled'] ? 'Yes' : 'No') . "<br>";
echo "API URL: " . (!empty($config['api_url']) ? $config['api_url'] : 'Not configured') . "<br>";
echo "API Key: " . (!empty($config['api_key']) ? 'Configured' : 'Not configured') . "<br>";
echo "Username: " . (!empty($config['username']) ? 'Configured' : 'Not configured') . "<br>";
echo "Password: " . (!empty($config['password']) ? 'Configured' : 'Not configured') . "<br>";
echo "Sender ID: " . $config['sender_id'] . "<br>";

// Check specific non-masking settings
echo "<h3>3. Non-Masking Settings in Database</h3>";
try {
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'non_musking_%'");
    $settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (empty($settings)) {
        echo "✗ No non-masking settings found in database<br>";
    } else {
        foreach ($settings as $setting) {
            echo $setting['setting_key'] . ": " . $setting['setting_value'] . "<br>";
        }
    }
} catch (Exception $e) {
    echo "✗ Error checking settings: " . $e->getMessage() . "<br>";
}

// Test cURL availability
echo "<h3>4. cURL Check</h3>";
if (function_exists('curl_version')) {
    $curlVersion = curl_version();
    echo "✓ cURL is available<br>";
    echo "Version: " . $curlVersion['version'] . "<br>";
} else {
    echo "✗ cURL is not available<br>";
}

echo "<h3>5. Next Steps</h3>";
if (!$config['enabled']) {
    echo "• Enable non-masking SMS in Admin Panel → Non-Masking Settings<br>";
}
if (empty($config['api_url'])) {
    echo "• Configure API URL in Admin Panel → Non-Masking Settings<br>";
}
if (empty($config['api_key'])) {
    echo "• Configure API Key in Admin Panel → Non-Masking Settings<br>";
}
if (empty($config['username'])) {
    echo "• Configure Username in Admin Panel → Non-Masking Settings<br>";
}
if (empty($config['password'])) {
    echo "• Configure Password in Admin Panel → Non-Masking Settings<br>";
}

echo "<br><a href='admin/non_musking_settings.php'>Go to Non-Masking Settings</a><br>";
echo "<a href='admin/test_non_musking_settings.php'>Test Non-Masking SMS</a><br>";
?>