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

// Company-provided MRAM SMS Configuration
$company_settings = [
    ['non_musking_enabled', '1'],  // Enable the service
    ['non_musking_method', 'sms'],
    ['non_musking_api_url', 'https://rest.mrmessaging.net/messages'],  // Working MRAM endpoint
    ['non_musking_api_key', 'C30027916974e980709412.23354057'],  // Company's API Key
    ['non_musking_username', 'C3002791'],  // Company's Username
    ['non_musking_password', 'hPOWSnw8rf'],  // Company's Password
    ['non_musking_sender_id', '8809601016579'],  // Company's Sender ID
    ['non_musking_tpl_general_message', 'প্রিয় {name}, {message}. ধন্যবাদ।'],
    ['non_musking_tpl_admission_confirmation', 'প্রিয় {name}, আপনার ভর্তি আবেদন সফলভাবে জমা দেওয়া হয়েছে। আবেদন নম্বর: {application_no}। ধন্যবাদ।'],
    ['non_musking_rate_limit', '60'],
    ['non_musking_max_retries', '3'],
    // Also update general SMS settings
    ['sms_enabled', '1'],
    ['sms_api_url', 'https://rest.mrmessaging.net/messages'],
    ['sms_api_key', 'C30027916974e980709412.23354057'],
    ['sms_username', 'C3002791'],
    ['sms_password', 'hPOWSnw8rf'],
    ['sms_sender_id', '8809601016579'],
    ['sms_tpl_admission_success', 'প্রিয় {name}, আপনার ভর্তি আবেদন সফলভাবে জমা দেওয়া হয়েছে। আবেদন নম্বর: {application_no}। ধন্যবাদ।'],
    ['sms_tpl_result_pass', 'প্রিয় {name}, আপনি পরীক্ষায় পাস করেছেন। গ্রেড: {grade}। ধন্যবাদ।'],
    ['sms_tpl_result_fail', 'প্রিয় {name}, আপনি পরীক্ষায় ফেল করেছেন। ধন্যবাদ।'],
    ['sms_tpl_fee_payment', 'প্রিয় {name}, আপনার ফি পেমেন্ট সফল হয়েছে। পরিমাণ: {paid_amount}। ধন্যবাদ।'],
    ['sms_tpl_fee_due_reminder', 'প্রিয় {name}, আপনার ফি বাকি রয়েছে। পরিমাণ: {due_amount}। জমা দিন।'],
    ['sms_tpl_bill_submission', 'প্রিয় {name}, আপনার বিল সফলভাবে জমা হয়েছে। নম্বর: {bill_number}। ধন্যবাদ।']
];

try {
    echo "<h2>Complete MRAM SMS Setup</h2>";
    
    $updated_count = 0;
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    foreach ($company_settings as $setting) {
        $stmt->execute([$setting[0], $setting[1], $setting[1]]);
        $updated_count++;
    }
    
    echo "✅ Successfully updated $updated_count settings<br>";
    echo "✅ API URLs updated to working MR Messaging endpoint: https://rest.mrmessaging.net/messages<br>";
    echo "✅ Company credentials configured<br>";
    
    // Show current settings
    $settings_stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE '%non_musking%' OR setting_key LIKE '%sms%'");
    $settings_stmt->execute();
    $settings = $settings_stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>All SMS Settings:</h3>";
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>Setting</th><th>Value</th></tr>";
    foreach ($settings as $setting) {
        $value = $setting['setting_key'] === 'non_musking_password' || $setting['setting_key'] === 'sms_password' ? '***HIDDEN***' : htmlspecialchars($setting['setting_value']);
        echo "<tr><td>" . htmlspecialchars($setting['setting_key']) . "</td><td>$value</td></tr>";
    }
    echo "</table><br>";
    
    echo "<h3>Next Steps:</h3>";
    echo "<ol>";
    echo "<li>Test the configuration again</li>";
    echo "<li>Visit Admin Panel → SMS Settings → Test Configuration</li>";
    echo "<li>Try sending a test SMS</li>";
    echo "</ol>";

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