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

echo "<h2>Auto-configuring MRAM SMS Service</h2>";

try {
    // Create settings table if it doesn't exist
    $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>";

    // MRAM SMS Configuration
    $mram_settings = [
        ['non_musking_enabled', '1'],  // Enable the service
        ['non_musking_method', 'sms'],
        ['non_musking_api_url', 'https://rest.mrmessaging.net/messages'],  // MRAM API endpoint
        ['non_musking_api_key', ''],  // Not using API key for MRAM
        ['non_musking_username', 'C3002791'],  // Your MRAM username
        ['non_musking_password', 'hPOWSnw8rf'],  // Your MRAM password
        ['non_musking_sender_id', 'SCHOOL'],  // Default 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']
    ];

    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");

    foreach ($mram_settings as $setting) {
        $stmt->execute([$setting[0], $setting[1], $setting[1]]);
        echo "✓ Configured: {$setting[0]} = {$setting[1]}<br>";
    }

    echo "<br><strong>✅ MRAM SMS Service configured successfully!</strong><br>";
    echo "<br><strong>Configuration Summary:</strong><br>";
    echo "• API URL: https://rest.mrmessaging.net/messages<br>";
    echo "• Authentication: HTTP Basic with your credentials<br>";
    echo "• Username: C3002791<br>";
    echo "• Password: hPOWSnw8rf<br>";
    echo "• Sender ID: SCHOOL<br>";
    echo "• Service: Enabled<br>";

    echo "<br><strong>Next Steps:</strong><br>";
    echo "1. <a href='admin/test_non_musking_settings.php'>Test the configuration</a><br>";
    echo "2. Try sending a test SMS to verify it works<br>";
    echo "3. The admission application system will now automatically send SMS confirmations<br>";

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