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

echo "=== SMS Notification Status Check ===\n";
echo "=====================================\n\n";

// Check if settings table exists
$stmt = $pdo->prepare("SHOW TABLES LIKE 'settings'");
$stmt->execute();
$tableExists = $stmt->fetch();

if (!$tableExists) {
    echo "❌ ERROR: Settings table does not exist!\n";
    exit(1);
}

echo "=== 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) {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $result = $stmt->fetch();
    
    if ($result) {
        $value = $result['setting_value'];
        $status = $value === '1' ? '✅ ENABLED' : '❌ DISABLED';
        echo "$description ($key): $status\n";
    } else {
        echo "$description ($key): ❌ NOT SET\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) {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $result = $stmt->fetch();
    
    if ($result) {
        $value = $result['setting_value'];
        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";
        }
    } else {
        echo "$description: ❌ NOT SET\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) {
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
    $stmt->execute([$key]);
    $result = $stmt->fetch();
    
    if ($result) {
        $value = $result['setting_value'];
        $status = !empty($value) ? '✅ SET' : '❌ EMPTY';
        echo "$description: $status\n";
    } else {
        echo "$description: ❌ NOT SET\n";
    }
}

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

// Check if Non-Musking is enabled
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_enabled'");
$stmt->execute();
$nonMuskingEnabled = $stmt->fetch();
$nonMuskingEnabled = $nonMuskingEnabled && $nonMuskingEnabled['setting_value'] === '1';

// Check if fee notifications are enabled
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_fee_notifications'");
$stmt->execute();
$feeNotificationsEnabled = $stmt->fetch();
$feeNotificationsEnabled = $feeNotificationsEnabled && $feeNotificationsEnabled['setting_value'] === '1';

// Check if result notifications are enabled
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_result_notifications'");
$stmt->execute();
$resultNotificationsEnabled = $stmt->fetch();
$resultNotificationsEnabled = $resultNotificationsEnabled && $resultNotificationsEnabled['setting_value'] === '1';

// Check if due reminders are enabled
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_fee_due_reminders'");
$stmt->execute();
$dueRemindersEnabled = $stmt->fetch();
$dueRemindersEnabled = $dueRemindersEnabled && $dueRemindersEnabled['setting_value'] === '1';

// Check if application confirmation is enabled
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_application_confirmation'");
$stmt->execute();
$applicationNotificationsEnabled = $stmt->fetch();
$applicationNotificationsEnabled = $applicationNotificationsEnabled && $applicationNotificationsEnabled['setting_value'] === '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=== Summary ===\n";
$allNotifications = [
    $nonMuskingEnabled,
    $feeNotificationsEnabled, 
    $resultNotificationsEnabled,
    $dueRemindersEnabled,
    $applicationNotificationsEnabled
];

$workingCount = count(array_filter($allNotifications));
$totalCount = count($allNotifications);

echo "Notifications working: $workingCount/$totalCount\n";
echo "Overall Status: " . ($workingCount == $totalCount ? "✅ ALL GOOD" : "❌ NEEDS FIXES") . "\n";

?>