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

echo "<h2>MRAM Developers Portal এর সাথে সিঙ্ক্রোনাইজেশন</h2>";
echo "<p><strong>রেফারেন্স URL:</strong> <a href='https://sms.mram.com.bd/Developers' target='_blank'>https://sms.mram.com.bd/Developers</a></p>";

// MRAM Developers 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',
    'rate_limit' => 60
];

echo "<h3>MRAM Developers Portal ক্রেডেনশিয়ালস:</h3>";
echo "<ul>";
echo "<li><strong>Username:</strong> {$mram_config['username']}</li>";
echo "<li><strong>API Key:</strong> {$mram_config['api_key']}</li>";
echo "<li><strong>Sender ID:</strong> {$mram_config['sender_id']}</li>";
echo "<li><strong>API URL:</strong> {$mram_config['api_url']}</li>";
echo "</ul>";

// Get current local configuration
$current_config = non_musking_config();

echo "<h3>বর্তমান লোকাল কনফিগারেশন:</h3>";
echo "<ul>";
echo "<li><strong>Enabled:</strong> " . ($current_config['enabled'] ? 'হ্যাঁ' : 'না') . "</li>";
echo "<li><strong>API URL:</strong> " . ($current_config['api_url'] ?? 'সেট নেই') . "</li>";
echo "<li><strong>Username:</strong> " . ($current_config['username'] ?? 'সেট নেই') . "</li>";
echo "<li><strong>API Key:</strong> " . (!empty($current_config['api_key']) ? substr($current_config['api_key'], 0, 10) . '...' : 'সেট নেই') . "</li>";
echo "<li><strong>Sender ID:</strong> " . ($current_config['sender_id'] ?? 'সেট নেই') . "</li>";
echo "</ul>";

// Check for mismatches
echo "<h3>তুলনা ফলাফল:</h3>";
$mismatches = [];

if (($current_config['username'] ?? '') !== $mram_config['username']) {
    $mismatches[] = ['non_musking_username', $current_config['username'] ?? 'NOT SET', $mram_config['username']];
    echo "<p style='color:red;'>❌ Username mismatch: Local='" . ($current_config['username'] ?? 'NOT SET') . "', MRAM='{$mram_config['username']}'</p>";
} else {
    echo "<p style='color:green;'>✅ Username matched</p>";
}

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

if (($current_config['sender_id'] ?? '') !== $mram_config['sender_id']) {
    $mismatches[] = ['non_musking_sender_id', $current_config['sender_id'] ?? 'NOT SET', $mram_config['sender_id']];
    echo "<p style='color:red;'>❌ Sender ID mismatch: Local='" . ($current_config['sender_id'] ?? 'NOT SET') . "', MRAM='{$mram_config['sender_id']}'</p>";
} else {
    echo "<p style='color:green;'>✅ Sender ID matched</p>";
}

if (($current_config['api_url'] ?? '') !== $mram_config['api_url']) {
    $mismatches[] = ['non_musking_api_url', $current_config['api_url'] ?? 'NOT SET', $mram_config['api_url']];
    echo "<p style='color:red;'>❌ API URL mismatch: Local='" . ($current_config['api_url'] ?? 'NOT SET') . "', MRAM='{$mram_config['api_url']}'</p>";
} else {
    echo "<p style='color:green;'>✅ API URL matched</p>";
}

if (($current_config['password'] ?? '') !== $mram_config['password']) {
    $mismatches[] = ['non_musking_password', 'NOT CHECKED (HIDDEN)', $mram_config['password']];
    echo "<p style='color:orange;'>ℹ️ Password not checked (hidden for security)</p>";
}

// Update database with MRAM credentials if mismatches exist
if (!empty($mismatches)) {
    echo "<h3>সিঙ্ক্রোনাইজেশন প্রক্রিয়া:</h3>";
    echo "<p>নিম্নলিখিত মানগুলো MRAM পোর্টালের সাথে মিল আনতে হালনাগাদ করা হবে:</p>";
    
    try {
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        
        // Update non-masking settings with MRAM values
        $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']]);
        
        // Also 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; font-size:18px; font-weight:bold;'>✅ সব সেটিংস MRAM Developers Portal এর সাথে সিঙ্ক্রোনাইজড!</p>";
        
        // Show updated configuration
        $updated_config = non_musking_config();
        echo "<h4>হালনাগাদকৃত কনফিগারেশন:</h4>";
        echo "<ul>";
        echo "<li><strong>Enabled:</strong> " . ($updated_config['enabled'] ? 'হ্যাঁ' : 'না') . "</li>";
        echo "<li><strong>API URL:</strong> " . ($updated_config['api_url'] ?? 'সেট নেই') . "</li>";
        echo "<li><strong>Username:</strong> " . ($updated_config['username'] ?? 'সেট নেই') . "</li>";
        echo "<li><strong>API Key:</strong> " . (!empty($updated_config['api_key']) ? substr($updated_config['api_key'], 0, 10) . '...' : 'সেট নেই') . "</li>";
        echo "<li><strong>Sender ID:</strong> " . ($updated_config['sender_id'] ?? 'সেট নেই') . "</li>";
        echo "</ul>";
        
    } catch (Exception $e) {
        echo "<p style='color:red;'>❌ সিঙ্ক্রোনাইজেশন ত্রুটি: " . $e->getMessage() . "</p>";
    }
} else {
    echo "<p style='color:green; font-size:18px; font-weight:bold;'>✅ সব সেটিংস ইতিমধ্যে MRAM Developers Portal এর সাথে মিলছে!</p>";
}

echo "<h3>একশন লিংকস:</h3>";
echo "<ul>";
echo "<li><a href='sms_settings_sync.php'>সিঙ্ক্রোনাইজড সেটিংস পেজ ওপেন করুন</a> - সেটিংস ম্যানুয়ালি চেক করুন</li>";
echo "<li><a href='simple_test_sms.php'>টেস্ট SMS পাঠান</a> - কাজ করছে কিনা টেস্ট করুন</li>";
echo "</ul>";

echo "<h3>নোট:</h3>";
echo "<p>এই প্রক্রিয়া MRAM Developers Portal (https://sms.mram.com.bd/Developers) এ প্রদত্ত ক্রেডেনশিয়ালস অনুযায়ী সব সেটিংস হালনাগাদ করেছে।</p>";
?>