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

echo "<h2>MRAM API and API Key Verification</h2>";

// Get current configuration
$config = non_musking_config();

echo "<h3>Current Configuration:</h3>";
echo "<ul>";
echo "<li>Enabled: " . ($config['enabled'] ? 'Yes' : 'No') . "</li>";
echo "<li>API URL: " . htmlspecialchars($config['api_url'] ?? 'Not set') . "</li>";
echo "<li>Username: " . (!empty($config['username']) ? htmlspecialchars($config['username']) : 'Not set') . "</li>";
echo "<li>API Key: " . (!empty($config['api_key']) ? substr(htmlspecialchars($config['api_key']), 0, 10) . '...' : 'Not set') . "</li>";
echo "<li>Sender ID: " . htmlspecialchars($config['sender_id'] ?? 'Not set') . "</li>";
echo "</ul>";

// Check credentials against MRAM
echo "<h3>Verifying MRAM Credentials</h3>";

// The company provided credentials
$expected_username = 'C3002791';
$expected_password = 'hPOWSnw8rf';
$expected_api_key = 'C30027916974e980709412.23354057';

echo "<p><strong>Expected Credentials:</strong></p>";
echo "<ul>";
echo "<li>Username: <strong>$expected_username</strong></li>";
echo "<li>Password: <strong>$expected_password</strong></li>";
echo "<li>API Key: <strong>$expected_api_key</strong></li>";
echo "</ul>";

echo "<p><strong>Your Configured Credentials:</strong></p>";
echo "<ul>";
echo "<li>Username: <strong>" . ($config['username'] ?? 'NOT SET') . "</strong></li>";
echo "<li>API Key: <strong>" . ($config['api_key'] ?? 'NOT SET') . "</strong></li>";
echo "</ul>";

$credentials_match = true;

if ($config['username'] !== $expected_username) {
    echo "<p style='color:red;'>❌ Username does not match! Expected: $expected_username, Got: " . ($config['username'] ?? 'NOT SET') . "</p>";
    $credentials_match = false;
} else {
    echo "<p style='color:green;'>✅ Username matches!</p>";
}

if ($config['api_key'] !== $expected_api_key) {
    echo "<p style='color:red;'>❌ API Key does not match! Expected: $expected_api_key, Got: " . ($config['api_key'] ?? 'NOT SET') . "</p>";
    $credentials_match = false;
} else {
    echo "<p style='color:green;'>✅ API Key matches!</p>";
}

if ($credentials_match) {
    echo "<div style='background-color:#d4edda; padding:15px; border-radius:5px; border:1px solid #c3e6cb;'>";
    echo "<h4 style='color:#155724;'>Credentials Verified Successfully!</h4>";
    echo "<p>Your MRAM credentials are correctly configured.</p>";
    echo "</div>";
} else {
    echo "<div style='background-color:#f8d7da; padding:15px; border-radius:5px; border:1px solid #f5c6cb;'>";
    echo "<h4 style='color:#721c24;'>Credentials Need Correction!</h4>";
    echo "<p>Please update your settings with the correct credentials:</p>";
    echo "<ul>";
    echo "<li>Username: <strong>$expected_username</strong></li>";
    echo "<li>API Key: <strong>$expected_api_key</strong></li>";
    echo "<li>Password: <strong>$expected_password</strong></li>";
    echo "</ul>";
    echo "</div>";
    
    // Provide SQL to fix the credentials
    echo "<h4>SQL Commands to Fix Credentials:</h4>";
    echo "<pre>";
    echo "UPDATE settings SET setting_value = '$expected_username' WHERE setting_key = 'non_musking_username';\n";
    echo "UPDATE settings SET setting_value = '$expected_api_key' WHERE setting_key = 'non_musking_api_key';\n";
    echo "UPDATE settings SET setting_value = '$expected_password' WHERE setting_key = 'non_musking_password';\n";
    echo "UPDATE settings SET setting_value = '$expected_username' WHERE setting_key = 'sms_username';\n";
    echo "UPDATE settings SET setting_value = '$expected_api_key' WHERE setting_key = 'sms_api_key';\n";
    echo "UPDATE settings SET setting_value = '$expected_password' WHERE setting_key = 'sms_password';\n";
    echo "</pre>";
}

// Show how to update settings
echo "<h3>How to Update Settings:</h3>";
echo "<ol>";
echo "<li>Go to Admin Panel → SMS Settings</li>";
echo "<li>Enter the correct credentials:</li>";
echo "<ul>";
echo "<li>Username: <strong>$expected_username</strong></li>";
echo "<li>API Key: <strong>$expected_api_key</strong></li>";
echo "<li>Password: <strong>$expected_password</strong></li>";
echo "<li>API URL: <strong>https://rest.mrmessaging.net/messages</strong></li>";
echo "</ul>";
echo "<li>Save the settings</li>";
echo "<li>Test the configuration</li>";
echo "</ol>";

?>