<?php
// Script to configure SMS API settings for MRAM gateway
// This script bypasses the HTTP_HOST requirement

// Define minimal constants to avoid errors
if (!defined('BASE_URL')) {
    define('BASE_URL', 'http://localhost/bidda');
}

// Database configuration
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_TIMEOUT => 30
    ]);

    echo "<h3>Configuring SMS API Settings for MRAM Gateway</h3>\n";

    // Configure MRAM SMS API settings
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Enable non-musking service
    $stmt->execute(['non_musking_enabled', '1', '1']);
    echo "<p>✅ Non-musking service enabled</p>\n";
    
    // Set MRAM API URL
    $stmt->execute(['non_musking_api_url', 'https://sms.mram.com.bd/smsapi', 'https://sms.mram.com.bd/smsapi']);
    echo "<p>✅ MRAM API URL configured</p>\n";
    
    // Set default sender ID (this should be updated with actual MRAM sender ID)
    $stmt->execute(['non_musking_sender_id', '8809601016579', '8809601016579']);
    echo "<p>✅ Sender ID configured</p>\n";
    
    // You need to update the API key with your actual MRAM API key
    // This is just a placeholder - you'll need to get your actual API key from MRAM
    $stmt->execute(['non_musking_api_key', 'your_actual_api_key_here', 'your_actual_api_key_here']);
    echo "<p>⚠️  API key set to placeholder - please update with your actual MRAM API key</p>\n";

    // Test mode settings (set to 0 to actually send SMS)
    $stmt->execute(['non_musking_test_mode', '0', '0']);  // Change to 1 for testing without sending actual SMS
    echo "<p>✅ Test mode disabled (will send actual SMS)</p>\n";

    // Configure templates
    $templates = [
        'non_musking_templates_general_message' => 'প্রিয় {name}, {message}. ধন্যবাদ।',
        'non_musking_templates_admission_confirmation' => 'প্রিয় {name}, আপনার ভর্তি আবেদন সফলভাবে জমা দেওয়া হয়েছে। আবেদন নম্বর: {application_no}। ধন্যবাদ।',
        'non_musking_templates_fee_payment' => 'প্রিয় {name}, আপনার ফি পেমেন্ট অফ {paid_amount} টাকা গ্রহণ করা হয়েছে। বকেয়া: {due_amount} টাকা। ধন্যবাদ।',
        'non_musking_templates_bank_transaction' => 'ব্যাংক ট্রানজেকশন পেমেন্ট রিসিভড {paid_amount} টাকা। বকেয়া: {due_amount} টাকা।',
        'non_musking_templates_bank_transaction_rejection' => 'ব্যাংক ট্রানজেকশন প্রত্যাখ্যান করা হয়েছে। পরিমাণ: {amount} টাকা। কারণ: {notes}'
    ];
    
    foreach ($templates as $key => $value) {
        $stmt->execute([$key, $value, $value]);
        echo "<p>✅ Template {$key} configured</p>\n";
    }

    // Enable bank transaction notifications
    $stmt->execute(['sms_send_bank_transaction_notifications', '1', '1']);
    echo "<p>✅ Bank transaction notifications enabled</p>\n";

    // Verify the settings
    echo "<h3>Verification:</h3>\n";
    
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_enabled'");
    $stmt->execute();
    $result = $stmt->fetch();
    $enabled = $result && $result['setting_value'] === '1' ? 'ENABLED' : 'DISABLED';
    echo "<p>Non-musking service: {$enabled}</p>\n";

    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_bank_transaction_notifications'");
    $stmt->execute();
    $result = $stmt->fetch();
    $notifications = $result && $result['setting_value'] === '1' ? 'ENABLED' : 'DISABLED';
    echo "<p>Bank transaction notifications: {$notifications}</p>\n";

    echo "<h4>Next Steps:</h4>\n";
    echo "<ol>\n";
    echo "<li>Replace 'your_actual_api_key_here' with your actual MRAM API key in the database</li>\n";
    echo "<li>Ensure your IP address is whitelisted in MRAM account settings</li>\n";
    echo "<li>Test the SMS functionality</li>\n";
    echo "</ol>\n";

    echo "<p><strong>SMS configuration completed. Bank transaction notifications should now work when properly configured.</strong></p>\n";

} catch (PDOException $e) {
    echo "Database connection failed: " . $e->getMessage();
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>