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

echo "<h2>Complete MRAM SMS Configuration Setup</h2>";

try {
    // Ensure settings table exists
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS settings (
            id INT PRIMARY KEY AUTO_INCREMENT,
            setting_key VARCHAR(100) UNIQUE,
            setting_value TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )
    ");
    echo "✓ Settings table verified<br>";

    // 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}। ধন্যবাদ।']
    ];

    $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]]);
        echo "✓ Configured: {$setting[0]} = {$setting[1]}<br>";
    }

    echo "<br><strong>✅ Complete MRAM SMS Service configured with company credentials!</strong><br>";
    echo "<br><strong>Configuration Summary:</strong><br>";
    echo "• API URL: https://rest.mrmessaging.net/messages<br>";
    echo "• API Key: C30027916974e980709412.23354057<br>";
    echo "• Username: C3002791<br>";
    echo "• Password: hPOWSnw8rf<br>";
    echo "• Sender ID: 8809601016579<br>";
    echo "• Service: Enabled<br>";

    echo "<br><strong>Important Notes:</strong><br>";
    echo "• Using both non-masking and general SMS settings<br>";
    echo "• Authentication will use Basic Auth with username:password<br>";
    echo "• All templates updated with Bengali messages<br>";

    echo "<br><strong>Next Steps:</strong><br>";
    echo "1. <a href='admin/sms_settings.php'>Review the SMS settings</a><br>";
    echo "2. <a href='admin/test_non_musking_settings.php'>Test the configuration</a><br>";
    echo "3. Try sending a test SMS to verify it works<br>";

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