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

echo "<h2>MRAM Portal & Local System Synchronization</h2>";

// MRAM Portal Configuration
$mram_config = [
    'username' => 'C3002791',
    'password' => 'hPOWSnw8rf',
    'api_key' => 'C30027916974e980709412.23354057',
    'sender_id' => '8809601016579',
    'api_url' => 'https://rest.mrmessaging.net/messages',
    'service_type' => 'non-masking',
    'supported_formats' => ['GSM-7', 'Unicode'],
    'rate_limit' => 60
];

echo "<h3>MRAM Portal Configuration:</h3>";
echo "<ul>";
echo "<li>Username: <strong>{$mram_config['username']}</strong></li>";
echo "<li>API Key: <strong>{$mram_config['api_key']}</strong></li>";
echo "<li>Sender ID: <strong>{$mram_config['sender_id']}</strong></li>";
echo "<li>API URL: <strong>{$mram_config['api_url']}</strong></li>";
echo "</ul>";

// Current local configuration
$local_config = non_musking_config();

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

// Compare configurations
echo "<h3>Configuration Comparison:</h3>";
$sync_needed = false;

if ($local_config['username'] !== $mram_config['username']) {
    echo "<p style='color:red;'>❌ Username mismatch: Local='{$local_config['username']}', MRAM='{$mram_config['username']}'</p>";
    $sync_needed = true;
} else {
    echo "<p style='color:green;'>✅ Username matched</p>";
}

if ($local_config['api_key'] !== $mram_config['api_key']) {
    echo "<p style='color:red;'>❌ API Key mismatch: Local='".(!empty($local_config['api_key']) ? substr($local_config['api_key'], 0, 10).'...' : 'NOT SET')."', MRAM='".substr($mram_config['api_key'], 0, 10).'...'."</p>";
    $sync_needed = true;
} else {
    echo "<p style='color:green;'>✅ API Key matched</p>";
}

if ($local_config['sender_id'] !== $mram_config['sender_id']) {
    echo "<p style='color:red;'>❌ Sender ID mismatch: Local='{$local_config['sender_id']}', MRAM='{$mram_config['sender_id']}'</p>";
    $sync_needed = true;
} else {
    echo "<p style='color:green;'>✅ Sender ID matched</p>";
}

if ($local_config['api_url'] !== $mram_config['api_url']) {
    echo "<p style='color:red;'>❌ API URL mismatch: Local='{$local_config['api_url']}', MRAM='{$mram_config['api_url']}'</p>";
    $sync_needed = true;
} else {
    echo "<p style='color:green;'>✅ API URL matched</p>";
}

if ($sync_needed) {
    echo "<div style='background-color:#f8d7da; padding:15px; border-radius:5px; border:1px solid #f5c6cb; margin-top:20px;'>";
    echo "<h4 style='color:#721c24;'>Sync Required!</h4>";
    echo "<p>Local configuration does not match MRAM portal. Updating local settings...</p>";
    
    try {
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        
        // Update non-masking settings
        $stmt->execute(['non_musking_enabled', '1', '1']);
        $stmt->execute(['non_musking_api_url', $mram_config['api_url'], $mram_config['api_url']]);
        $stmt->execute(['non_musking_api_key', $mram_config['api_key'], $mram_config['api_key']]);
        $stmt->execute(['non_musking_username', $mram_config['username'], $mram_config['username']]);
        $stmt->execute(['non_musking_password', $mram_config['password'], $mram_config['password']]);
        $stmt->execute(['non_musking_sender_id', $mram_config['sender_id'], $mram_config['sender_id']]);
        
        // Update general SMS settings
        $stmt->execute(['sms_enabled', '1', '1']);
        $stmt->execute(['sms_api_url', $mram_config['api_url'], $mram_config['api_url']]);
        $stmt->execute(['sms_api_key', $mram_config['api_key'], $mram_config['api_key']]);
        $stmt->execute(['sms_username', $mram_config['username'], $mram_config['username']]);
        $stmt->execute(['sms_password', $mram_config['password'], $mram_config['password']]);
        $stmt->execute(['sms_sender_id', $mram_config['sender_id'], $mram_config['sender_id']]);
        
        echo "<p style='color:green;'>✅ All settings synchronized with MRAM portal!</p>";
        
        // Show updated configuration
        $updated_config = non_musking_config();
        echo "<h4>Updated Configuration:</h4>";
        echo "<ul>";
        echo "<li>Enabled: <strong>" . ($updated_config['enabled'] ? 'Yes' : 'No') . "</strong></li>";
        echo "<li>API URL: <strong>" . ($updated_config['api_url'] ?? 'Not set') . "</strong></li>";
        echo "<li>Username: <strong>" . ($updated_config['username'] ?? 'Not set') . "</strong></li>";
        echo "<li>API Key: <strong>" . (!empty($updated_config['api_key']) ? substr($updated_config['api_key'], 0, 10) . '...' : 'Not set') . "</strong></li>";
        echo "<li>Sender ID: <strong>" . ($updated_config['sender_id'] ?? 'Not set') . "</strong></li>";
        echo "</ul>";
        
    } catch (Exception $e) {
        echo "<p style='color:red;'>❌ Error updating settings: " . $e->getMessage() . "</p>";
    }
    
    echo "</div>";
} else {
    echo "<div style='background-color:#d4edda; padding:15px; border-radius:5px; border:1px solid #c3e6cb; margin-top:20px;'>";
    echo "<h4 style='color:#155724;'>Perfect Match!</h4>";
    echo "<p>Local configuration is already synchronized with MRAM portal.</p>";
    echo "</div>";
}

echo "<h3>Next Steps:</h3>";
echo "<ol>";
echo "<li>Test the configuration in Admin Panel → SMS Settings</li>";
echo "<li>Try sending a test SMS</li>";
echo "<li>If still having issues, contact MRAM support to confirm API access</li>";
echo "</ol>";

?>