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

echo "<h2>MRAM SMS Authentication Diagnostic</h2>";

// Get current settings
$stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'non_musking_%'");
$stmt->execute();
$currentSettings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

echo "<h3>Current MRAM SMS Settings:</h3>";
echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<tr><th>Setting</th><th>Value</th></tr>";
foreach ($currentSettings as $key => $value) {
    echo "<tr><td>" . htmlspecialchars($key) . "</td><td>" . htmlspecialchars($value) . "</td></tr>";
}
echo "</table><br>";

// Validate credentials
$username = $currentSettings['non_musking_username'] ?? '';
$password = $currentSettings['non_musking_password'] ?? '';
$api_key = $currentSettings['non_musking_api_key'] ?? '';

echo "<h3>Credential Validation:</h3>";
echo "Username: " . (!empty($username) ? "SET ($username)" : "<span style='color:red;'>MISSING</span>") . "<br>";
echo "Password: " . (!empty($password) ? "SET (hidden)" : "<span style='color:red;'>MISSING</span>") . "<br>";
echo "API Key: " . (!empty($api_key) ? "SET (hidden)" : "NOT SET") . "<br>";

// Validate endpoint
$api_url = $currentSettings['non_musking_api_url'] ?? '';
echo "API URL: " . (!empty($api_url) ? $api_url : "<span style='color:red;'>MISSING</span>") . "<br>";

// Expected credentials based on project memory
echo "<br><h3>Expected Credentials (from project memory):</h3>";
echo "Username: C3002791<br>";
echo "Password: hPOWSnw8rf<br>";
echo "API Key: C30027916974e980709412.23354057<br>";
echo "API URL: https://rest.mrmessaging.net/messages<br>";

// Check if credentials match expected values
$expected_username = 'C3002791';
$expected_password = 'hPOWSnw8rf';
$expected_api_key = 'C30027916974e980709412.23354057';

if ($username !== $expected_username || $password !== $expected_password) {
    echo "<br><h3 style='color:red;'>⚠️ Credential Mismatch Detected!</h3>";
    echo "<p>Current credentials do not match expected values. Updating...</p>";
    
    try {
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        
        $stmt->execute(['non_musking_username', $expected_username, $expected_username]);
        $stmt->execute(['non_musking_password', $expected_password, $expected_password]);
        $stmt->execute(['non_musking_api_key', $expected_api_key, $expected_api_key]);
        
        echo "<p style='color:green;'>✅ Credentials updated to expected values!</p>";
    } catch (Exception $e) {
        echo "<p style='color:red;'>❌ Error updating credentials: " . $e->getMessage() . "</p>";
    }
} else {
    echo "<p style='color:green;'>✅ Credentials match expected values!</p>";
}

echo "<br><h3>Next Steps:</h3>";
echo "<ol>";
echo "<li>Verify that your MRAM account is active and has sufficient balance</li>";
echo "<li>Check that the credentials are entered correctly in your MRAM account</li>";
echo "<li>Ensure that your IP address is whitelisted if required</li>";
echo "<li>Test the configuration again</li>";
echo "</ol>";

?>