<?php
echo "<h2>Fix MRAM API IP Address Issue</h2>";

echo "<h3>Error Code 1016: IP address not allowed!!</h3>";
echo "<p>This error means your server's IP address is not whitelisted in your MRAM account.</p>";

// The correct API key from MRAM Developers portal
$correct_api_key = 'C30027916975f051ed0e01.45424521';

// Database connection
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]);

    // Update the API settings to ensure they're correct
    $update_stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Update API keys
    $update_stmt->execute(['non_musking_api_key', $correct_api_key, $correct_api_key]);
    $update_stmt->execute(['sms_api_key', $correct_api_key, $correct_api_key]);
    
    // Update API URL to the correct one
    $update_stmt->execute(['non_musking_api_url', 'https://sms.mram.com.bd/smsapi', 'https://sms.mram.com.bd/smsapi']);
    $update_stmt->execute(['sms_api_url', 'https://sms.mram.com.bd/smsapi', 'https://sms.mram.com.bd/smsapi']);
    
    echo "<h3>✅ Updated API settings in database</h3>";

} catch (Exception $e) {
    echo "<p style='color: red;'>Database error: " . $e->getMessage() . "</p>";
}

echo "<h3 style='color:red;'>🚨 CRITICAL ISSUE: IP Address Not Whitelisted</h3>";
echo "<p>To fix Error Code 1016, you need to whitelist your server's IP address in your MRAM account:</p>";

// Get the current server IP
$server_ip = $_SERVER['SERVER_ADDR'] ?? $_SERVER['LOCAL_ADDR'] ?? getHostByName(getHostName());
if (filter_var($server_ip, FILTER_VALIDATE_IP) === false) {
    $server_ip = 'YOUR_SERVER_IP_ADDRESS';
}

echo "<h3>Your Server IP Address:</h3>";
echo "<p><strong>Server IP:</strong> $server_ip</p>";

echo "<h3>Steps to Fix:</h3>";
echo "<ol>";
echo "<li><strong>Login to MRAM Portal:</strong> Go to https://sms.mram.com.bd/</li>";
echo "<li><strong>Navigate to Settings:</strong> Look for 'IP Management' or 'Security Settings'</li>";
echo "<li><strong>Add Your IP:</strong> Add this IP address to your allowed IPs: <strong>$server_ip</strong></li>";
echo "<li><strong>Contact MRAM Support:</strong> If you can't find the IP management section, contact MRAM support at their office</li>";
echo "<li><strong>Alternative:</strong> You can also use their API to add IP: https://sms.mram.com.bd/getkey/(username)/(password) - but IP whitelisting is usually done through the web portal</li>";
echo "</ol>";

echo "<h3>API Key Information:</h3>";
echo "<p><strong>Current API Key:</strong> $correct_api_key</p>";
echo "<p><strong>API URL:</strong> https://sms.mram.com.bd/smsapi</p>";

echo "<h3>MRAM API Error Codes Reference:</h3>";
echo "<ul>";
echo "<li><strong>1002:</strong> Sender Id/Masking Not Found</li>";
echo "<li><strong>1003:</strong> API Not Found</li>";
echo "<li><strong>1004:</strong> SPAM Detected</li>";
echo "<li><strong>1005:</strong> Internal Error</li>";
echo "<li><strong>1007:</strong> Balance Insufficient</li>";
echo "<li><strong>1008:</strong> Message is empty</li>";
echo "<li><strong>1010:</strong> Invalid User & Password</li>";
echo "<li><strong>1012:</strong> Invalid Number</li>";
echo "<li><strong>1016:</strong> IP address not allowed!! (Current Issue)</li>";
echo "</ul>";

echo "<h3>Next Steps:</h3>";
echo "<ul>";
echo "<li>1. <strong>WHITELIST YOUR IP ADDRESS</strong> in MRAM portal</li>";
echo "<li>2. Wait 5-10 minutes for the changes to take effect</li>";
echo "<li>3. Test SMS sending again</li>";
echo "<li>4. Contact MRAM support if you need help with IP whitelisting</li>";
echo "</ul>";

echo "<p style='color:blue; font-weight:bold;'>Note: This is an account-level security setting that must be configured in your MRAM account dashboard.</p>";
?>