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

header('Content-Type: text/plain; charset=UTF-8');

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

// Check database settings
echo "Database Settings:\n";
echo "------------------\n";
$stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE '%sms%' OR setting_key LIKE '%non_musking%' ORDER BY setting_key");
$settings = [];
while($row = $stmt->fetch()) {
    $settings[$row['setting_key']] = $row['setting_value'];
    echo $row['setting_key'] . ': ' . $row['setting_value'] . "\n";
}

echo "\nRequired Settings Check:\n";
echo "------------------------\n";
$required = [
    'non_musking_enabled' => 'Non-Musking SMS Enabled',
    'sms_send_fee_notifications' => 'Fee Notifications Enabled',
    'non_musking_api_key' => 'MRAM API Key',
    'non_musking_api_url' => 'MRAM API URL',
    'non_musking_sender_id' => 'Sender ID'
];

foreach ($required as $key => $description) {
    $value = $settings[$key] ?? 'NOT SET';
    $status = ($value !== 'NOT SET' && $value !== '' && $value !== '0') ? '✅ OK' : '❌ MISSING';
    echo "$description: $status ($value)\n";
}

echo "\nConfiguration Objects:\n";
echo "----------------------\n";

// Test SMS config
$config = sms_config();
echo "SMS Config:\n";
echo "  Enabled: " . ($config['enabled'] ? 'Yes' : 'No') . "\n";
echo "  Test Mode: " . ($config['test_mode'] ? 'Yes' : 'No') . "\n";
echo "  Send to Guardian: " . ($config['send_to_guardian_also'] ? 'Yes' : 'No') . "\n";

// Test Non-Musking config
$non_musking_config = non_musking_config();
echo "\nNon-Musking Config:\n";
echo "  Enabled: " . ($non_musking_config['enabled'] ? 'Yes' : 'No') . "\n";
echo "  API URL: " . ($non_musking_config['api_url'] ?? 'Not Set') . "\n";
echo "  API Key: " . (!empty($non_musking_config['api_key']) ? 'Set' : 'Not Set') . "\n";
echo "  Sender ID: " . ($non_musking_config['sender_id'] ?? 'Not Set') . "\n";
echo "  Method: " . ($non_musking_config['method'] ?? 'Not Set') . "\n";

// Check templates
echo "\nTemplates:\n";
echo "----------\n";
echo "Fee Payment Template: " . ($config['templates']['fee_payment'] ?? 'Not Found') . "\n";
echo "Non-Musking Fee Payment Template: " . ($non_musking_config['templates']['fee_payment'] ?? 'Not Found') . "\n";

echo "\nFee Notification Status:\n";
echo "------------------------\n";
$fee_notifications_enabled = $settings['sms_send_fee_notifications'] ?? '0';
$non_musking_enabled = $settings['non_musking_enabled'] ?? '0';

if ($fee_notifications_enabled === '1' && $non_musking_enabled === '1') {
    echo "✅ Fee notifications are ENABLED and Non-Musking SMS is ENABLED\n";
    echo "✅ Fee collection should send SMS notifications\n";
} else {
    echo "❌ Fee notifications status:\n";
    echo "   - Fee Notifications Enabled: " . ($fee_notifications_enabled === '1' ? 'Yes' : 'No') . "\n";
    echo "   - Non-Musking SMS Enabled: " . ($non_musking_enabled === '1' ? 'Yes' : 'No') . "\n";
    if ($fee_notifications_enabled !== '1') {
        echo "   FIX: Enable 'Fee Notifications' in admin/sms_notification_settings.php\n";
    }
    if ($non_musking_enabled !== '1') {
        echo "   FIX: Enable 'Non-Musking SMS' in admin/sms_settings.php\n";
    }
}

echo "\n=== End Debug ===\n";
?>