<?php
// Check all SMS templates and settings
$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 "=== Comprehensive SMS Configuration Check ===\n";

    // Get all SMS related settings
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE '%sms%' OR setting_key LIKE '%non_musking%' ORDER BY setting_key");
    $stmt->execute();
    $settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $config = [];
    foreach ($settings as $setting) {
        $config[$setting['setting_key']] = $setting['setting_value'];
    }

    echo "\n=== 1. SMS Notification Settings ===\n";
    $notificationSettings = [
        'sms_notification_enabled' => 'SMS Notifications Master Switch',
        'sms_send_application_confirmation' => 'Application Confirmation',
        'sms_send_fee_notifications' => 'Fee Notifications',
        'sms_send_result_notifications' => 'Result Notifications',
        'sms_send_fee_due_reminders' => 'Fee Due Reminders'
    ];

    foreach ($notificationSettings as $key => $description) {
        $value = $config[$key] ?? 'NOT SET';
        $status = $value === '1' ? '✅ ENABLED' : '❌ DISABLED';
        echo "$description ($key): $status ($value)\n";
    }

    echo "\n=== 2. Non-Musking SMS Service Configuration ===\n";
    $nonMuskingSettings = [
        'non_musking_enabled' => 'Service Status',
        'non_musking_api_key' => 'API Key',
        'non_musking_username' => 'Username',
        'non_musking_password' => 'Password',
        'non_musking_sender_id' => 'Sender ID',
        'non_musking_url' => 'API URL',
        'non_musking_method' => 'HTTP Method'
    ];

    foreach ($nonMuskingSettings as $key => $description) {
        $value = $config[$key] ?? 'NOT SET';
        if (in_array($key, ['non_musking_api_key', 'non_musking_password', 'non_musking_username']) && !empty($value)) {
            $value = substr($value, 0, 5) . '...' . substr($value, -3);
        }
        if ($key === 'non_musking_enabled') {
            $status = $value === '1' ? '✅ ENABLED' : '❌ DISABLED';
            echo "$description: $status\n";
        } else {
            echo "$description: $value\n";
        }
    }

    echo "\n=== 3. SMS Templates Status ===\n";
    $templates = [
        'sms_tpl_application_confirmation' => 'Application Confirmation',
        'sms_tpl_fee_payment' => 'Fee Payment',
        'sms_tpl_fee_due_reminder' => 'Fee Due Reminder',
        'sms_tpl_result_pass' => 'Result Pass',
        'sms_tpl_result_fail' => 'Result Fail',
        'non_musking_templates_fee_payment' => 'Non-Musking Fee Payment',
        'non_musking_templates_general_message' => 'General Message'
    ];

    foreach ($templates as $key => $description) {
        $value = $config[$key] ?? '';
        $status = !empty($value) ? '✅ SET' : '❌ EMPTY';
        echo "$description: $status\n";
        if (!empty($value) && strlen($value) < 100) { // Show short templates
            echo "  Template: $value\n";
        }
    }

    echo "\n=== 4. Critical Status Check ===\n";

    // Check if Non-Musking is enabled
    $nonMuskingEnabled = ($config['non_musking_enabled'] ?? '0') === '1';

    // Check if fee notifications are enabled
    $feeNotificationsEnabled = ($config['sms_send_fee_notifications'] ?? '0') === '1';

    // Check if result notifications are enabled
    $resultNotificationsEnabled = ($config['sms_send_result_notifications'] ?? '0') === '1';

    // Check if due reminders are enabled
    $dueRemindersEnabled = ($config['sms_send_fee_due_reminders'] ?? '0') === '1';

    // Check if application confirmation is enabled
    $applicationNotificationsEnabled = ($config['sms_send_application_confirmation'] ?? '0') === '1';

    echo "Fee Notifications (Collect Fee): " . 
         ($nonMuskingEnabled && $feeNotificationsEnabled ? "✅ WORKING" : "❌ NOT WORKING") . "\n";
    echo "Result Notifications (Publish Result): " . 
         ($nonMuskingEnabled && $resultNotificationsEnabled ? "✅ WORKING" : "❌ NOT WORKING") . "\n";
    echo "Due Reminders (Send Reminders): " . 
         ($nonMuskingEnabled && $dueRemindersEnabled ? "✅ WORKING" : "❌ NOT WORKING") . "\n";
    echo "Application Confirmations (New Applications): " . 
         ($nonMuskingEnabled && $applicationNotificationsEnabled ? "✅ WORKING" : "❌ NOT WORKING") . "\n";

    echo "\n=== 5. Action Required ===\n";

    if (!$nonMuskingEnabled) {
        echo "❌ Fix: Enable Non-Musking SMS in admin/sms_settings.php\n";
    }

    if (!$feeNotificationsEnabled) {
        echo "❌ Fix: Enable Fee Notifications in admin/sms_notification_settings.php\n";
    }

    if (!$resultNotificationsEnabled) {
        echo "❌ Fix: Enable Result Notifications in admin/sms_notification_settings.php\n";
    }

    if (!$dueRemindersEnabled) {
        echo "❌ Fix: Enable Due Reminders in admin/sms_notification_settings.php\n";
    }

    if (!$applicationNotificationsEnabled) {
        echo "❌ Fix: Enable Application Confirmation in admin/sms_notification_settings.php\n";
    }

    echo "\n=== 6. Template Issues ===\n";
    
    // Check specific templates for application confirmations
    $appTemplate = $config['sms_tpl_application_confirmation'] ?? '';
    $feeTemplate = $config['sms_tpl_fee_payment'] ?? '';
    $resultPassTemplate = $config['sms_tpl_result_pass'] ?? '';
    $resultFailTemplate = $config['sms_tpl_result_fail'] ?? '';
    $dueReminderTemplate = $config['sms_tpl_fee_due_reminder'] ?? '';
    
    if (!empty($appTemplate)) {
        echo "✅ Application Confirmation Template: SET\n";
    } else {
        echo "❌ Application Confirmation Template: MISSING\n";
    }
    
    if (!empty($feeTemplate)) {
        echo "✅ Fee Payment Template: SET\n";
    } else {
        echo "❌ Fee Payment Template: MISSING\n";
    }
    
    if (!empty($resultPassTemplate)) {
        echo "✅ Result Pass Template: SET\n";
    } else {
        echo "❌ Result Pass Template: MISSING\n";
    }
    
    if (!empty($resultFailTemplate)) {
        echo "✅ Result Fail Template: SET\n";
    } else {
        echo "❌ Result Fail Template: MISSING\n";
    }
    
    if (!empty($dueReminderTemplate)) {
        echo "✅ Fee Due Reminder Template: SET\n";
    } else {
        echo "❌ Fee Due Reminder Template: MISSING\n";
    }

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