<?php
// Test SMS Configuration for Fee Notifications
// Using the same database credentials as config/database.php
$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,
    ]);

    echo "=== Testing SMS Configuration ===\n";

    // Check current SMS settings
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('sms_notification_enabled', 'sms_send_fee_notifications', 'non_musking_enabled')");
    $stmt->execute();
    $settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "Current Settings:\n";
    foreach ($settings as $setting) {
        echo "  {$setting['setting_key']}: {$setting['setting_value']}\n";
    }

    // Enable fee notifications if not already enabled
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Enable SMS notification master switch
    $stmt->execute(['sms_notification_enabled', '1', '1']);
    echo "✅ SMS Notifications Master Switch enabled\n";

    // Enable fee notifications specifically
    $stmt->execute(['sms_send_fee_notifications', '1', '1']);
    echo "✅ Fee Notifications enabled\n";

    // Ensure non-musking SMS is enabled
    $stmt->execute(['non_musking_enabled', '1', '1']);
    echo "✅ Non-Musking SMS Service enabled\n";

    // Check if fee payment template exists
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_templates_fee_payment'");
    $stmt->execute();
    $template = $stmt->fetch();
    
    if (!$template || empty($template['setting_value'])) {
        // Set default fee payment template
        $defaultTemplate = "প্রিয় {name}, আপনার ফি {paid_amount} টাকা গৃহীত হয়েছে। বর্তমান বকেয়া: {due_amount} টাকা। ধন্যবাদ।";
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute(['non_musking_templates_fee_payment', $defaultTemplate, $defaultTemplate]);
        echo "✅ Fee payment template set: $defaultTemplate\n";
    } else {
        echo "✅ Fee payment template already exists\n";
    }

    echo "\n=== Verification ===\n";
    
    // Verify the settings
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('sms_notification_enabled', 'sms_send_fee_notifications', 'non_musking_enabled')");
    $stmt->execute();
    $settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($settings as $setting) {
        $status = $setting['setting_value'] === '1' ? '✅ ENABLED' : '❌ DISABLED';
        echo "{$setting['setting_key']}: $status\n";
    }

    echo "\n=== Final Status ===\n";
    echo "✅ Fee notifications are now enabled for https://zashof.com/fees/student_fees.php\n";
    echo "✅ Students will receive SMS when fees are collected\n";

} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
}
?>