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

echo "<h2>MRAM SMS Service Configuration Update</h2>";
echo "<div style='background-color: #fff3cd; padding: 15px; border: 1px solid #ffeaa7; border-radius: 5px; margin: 10px 0;'>";

echo "<h3>⚠️ Important Notice</h3>";
echo "<p>The API is returning authentication errors because:</p>";
echo "<ul>";
echo "<li>Your current credentials (C3002791/hPOWSnw8rf) appear to be for the web panel</li>";
echo "<li>MRAM's API v3 requires an <strong>API Key</strong>, not username/password</li>";
echo "<li>You need to obtain the actual API Key from your MRAM account</li>";
echo "</ul>";

echo "<h3>Steps to Get Your API Key:</h3>";
echo "<ol>";
echo "<li>Login to your MRAM account at <a href='https://sms.mram.com.bd' target='_blank'>sms.mram.com.bd</a></li>";
echo "<li>Look for 'API' or 'Developer' section in your account dashboard</li>";
echo "<li>Generate or find your API Key (it should be a long alphanumeric string)</li>";
echo "<li>Copy the API Key</li>";
echo "</ol>";

echo "</div>";

try {
    // Update configuration to use API Key instead
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Clear username/password since they're not used for API Key auth
    $stmt->execute(['non_musking_username', '', '']);
    $stmt->execute(['non_musking_password', '', '']);
    
    // Set API Key placeholder (user needs to update this)
    $stmt->execute(['non_musking_api_key', 'YOUR_API_KEY_HERE', 'YOUR_API_KEY_HERE']);
    
    echo "✓ Configuration updated to use API Key authentication<br>";
    echo "✓ Username/Password fields cleared<br>";
    echo "✓ API Key field set to placeholder<br>";
    
    echo "<br><h3>Current Configuration Status:</h3>";
    echo "• API URL: https://rest.mrmessaging.net/messages ✓<br>";
    echo "• Authentication: API Key (Bearer token) ✓<br>";
    echo "• Username: Cleared (not needed) ✓<br>";
    echo "• Password: Cleared (not needed) ✓<br>";
    echo "• API Key: <span style='color: red;'>NEEDS YOUR ACTUAL API KEY</span> ✗<br>";
    echo "• Sender ID: SCHOOL ✓<br>";
    
    echo "<br><h3>What You Need To Do:</h3>";
    echo "<ol>";
    echo "<li>Get your API Key from MRAM dashboard</li>";
    echo "<li>Go to Admin Panel → Non-Masking SMS Settings</li>";
    echo "<li>Enter your API Key in the 'API Key' field</li>";
    echo "<li>Save the settings</li>";
    echo "<li>Test the configuration</li>";
    echo "</ol>";
    
    echo "<br><a href='admin/non_musking_settings.php' style='background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>Go to SMS Settings</a>";
    echo "&nbsp;&nbsp;<a href='admin/test_non_musking_settings.php' style='background-color: #28a745; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>Test Configuration</a>";

} catch (Exception $e) {
    echo "<strong>❌ Error:</strong> " . $e->getMessage() . "<br>";
}
?>