<?php
// Final SMS Configuration Verification
$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 "=== Final SMS Configuration Verification ===\n";

    // Get all critical settings
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('non_musking_enabled', 'non_musking_url', 'non_musking_api_key', 'non_musking_sender_id', 'sms_notification_enabled', 'sms_send_application_confirmation', 'sms_send_fee_notifications', 'sms_send_result_notifications', 'sms_send_fee_due_reminders', 'sms_tpl_application_confirmation', 'sms_tpl_fee_payment', 'sms_tpl_result_pass', 'sms_tpl_result_fail', 'sms_tpl_fee_due_reminder')");
    $stmt->execute();
    $settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $config = [];
    foreach ($settings as $setting) {
        $config[$setting['setting_key']] = $setting['setting_value'];
    }

    echo "\n=== 1. SMS Service Configuration ===\n";
    $serviceSettings = [
        'non_musking_enabled' => 'Non-Musking Service',
        'non_musking_url' => 'API URL',
        'non_musking_api_key' => 'API Key',
        'non_musking_sender_id' => 'Sender ID',
        'non_musking_method' => 'HTTP Method'
    ];
    
    foreach ($serviceSettings as $key => $description) {
        $value = $config[$key] ?? ($key === 'non_musking_method' ? 'sms' : '');
        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' ? '✅ ACTIVE' : '❌ INACTIVE';
            echo "$description: $status ($value)\n";
        } else {
            $status = !empty($value) ? '✅ SET' : '❌ EMPTY';
            echo "$description: $status ($value)\n";
        }
    }

    echo "\n=== 2. Notification Type Status ===\n";
    $notificationSettings = [
        'sms_notification_enabled' => 'Master Switch',
        'sms_send_application_confirmation' => 'Application Confirmations',
        '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] ?? '0';
        $status = $value === '1' ? '✅ ENABLED' : '❌ DISABLED';
        echo "$description: $status ($value)\n";
    }

    echo "\n=== 3. Template Availability ===\n";
    $templateSettings = [
        'sms_tpl_application_confirmation' => 'Application Confirmation',
        'sms_tpl_fee_payment' => 'Fee Payment',
        'sms_tpl_result_pass' => 'Result Pass',
        'sms_tpl_result_fail' => 'Result Fail',
        'sms_tpl_fee_due_reminder' => 'Fee Due Reminder'
    ];

    foreach ($templateSettings as $key => $description) {
        $value = $config[$key] ?? '';
        $status = !empty($value) ? '✅ AVAILABLE' : '❌ MISSING';
        echo "$description: $status\n";
    }

    echo "\n=== 4. All Services Status ===\n";
    $services = [
        'Application Confirmations' => ['non_musking_enabled', 'sms_send_application_confirmation'],
        'Fee Notifications' => ['non_musking_enabled', 'sms_send_fee_notifications'],
        'Result Notifications' => ['non_musking_enabled', 'sms_send_result_notifications'],
        'Fee Due Reminders' => ['non_musking_enabled', 'sms_send_fee_due_reminders']
    ];

    foreach ($services as $serviceName => $reqSettings) {
        $enabled = true;
        foreach ($reqSettings as $setting) {
            $value = $config[$setting] ?? '0';
            if ($setting === 'non_musking_enabled') {
                $enabled = $enabled && ($value === '1');
            } else {
                $enabled = $enabled && ($value === '1');
            }
        }
        
        // Also check if API URL is set
        $apiUrlSet = !empty($config['non_musking_url']);
        $enabled = $enabled && $apiUrlSet;
        
        $status = $enabled ? '✅ WORKING' : '❌ NOT WORKING';
        echo "$serviceName: $status\n";
    }

    echo "\n=== 5. Summary ===\n";
    $allWorking = true;
    foreach ($services as $serviceName => $reqSettings) {
        $enabled = true;
        foreach ($reqSettings as $setting) {
            $value = $config[$setting] ?? '0';
            if ($setting === 'non_musking_enabled') {
                $enabled = $enabled && ($value === '1');
            } else {
                $enabled = $enabled && ($value === '1');
            }
        }
        
        $apiUrlSet = !empty($config['non_musking_url']);
        $enabled = $enabled && $apiUrlSet;
        
        if (!$enabled) {
            $allWorking = false;
        }
    }

    if ($allWorking) {
        echo "🎉 ALL SMS SERVICES ARE NOW WORKING PROPERLY!\n";
        echo "✅ Application confirmations\n";
        echo "✅ Fee notifications\n";
        echo "✅ Result notifications\n";
        echo "✅ Fee due reminders\n";
        echo "\nThe issue has been resolved. All SMS notifications should now work on https://zashof.com/admin/sms_settings.php\n";
    } else {
        echo "❌ Some services are still not working. Please check configuration.\n";
    }

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