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

echo "<h2>Fee SMS Notification Debug</h2>";

try {
    // Check all relevant SMS settings
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE '%sms%' OR setting_key LIKE '%fee%' ORDER BY setting_key");
    $stmt->execute();
    $settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>Current Database Settings:</h3>";
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>Setting Key</th><th>Value</th><th>Status</th></tr>";
    
    $required_settings = [
        'sms_enabled' => 'Main SMS Service',
        'sms_send_fee_notifications' => 'Fee Notifications',
        'non_musking_enabled' => 'Non-Masking SMS'
    ];
    
    foreach ($settings as $setting) {
        $key = $setting['setting_key'];
        $value = $setting['setting_value'];
        $status = '';
        
        if (isset($required_settings[$key])) {
            if ($value === '1') {
                $status = '✅ ENABLED';
            } else {
                $status = '❌ DISABLED';
            }
        }
        
        echo "<tr>";
        echo "<td>" . htmlspecialchars($key) . "</td>";
        echo "<td>" . htmlspecialchars($value) . "</td>";
        echo "<td>" . $status . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    // Check if all required settings are enabled
    echo "<h3>Verification:</h3>";
    $all_enabled = true;
    
    foreach ($required_settings as $key => $description) {
        $found = false;
        $enabled = false;
        
        foreach ($settings as $setting) {
            if ($setting['setting_key'] === $key) {
                $found = true;
                $enabled = ($setting['setting_value'] === '1');
                break;
            }
        }
        
        if (!$found) {
            echo "<p>⚠️ $description ($key): NOT FOUND in database</p>";
            $all_enabled = false;
        } elseif (!$enabled) {
            echo "<p>❌ $description ($key): DISABLED</p>";
            $all_enabled = false;
        } else {
            echo "<p>✅ $description ($key): ENABLED</p>";
        }
    }
    
    if ($all_enabled) {
        echo "<h3 style='color: green;'>✅ All required settings are enabled!</h3>";
        echo "<p>Fee SMS notifications should be working. Let's test the actual sending process...</p>";
        
        // Test the fee collection SMS sending logic
        echo "<h3>Testing Fee Collection SMS Logic:</h3>";
        
        // Simulate a fee collection scenario
        $test_student_id = 1; // Use a test student ID
        
        // Check if student exists and has phone number
        $stmt = $pdo->prepare("SELECT phone, guardian_phone, first_name, last_name FROM students WHERE id = ?");
        $stmt->execute([$test_student_id]);
        $student = $stmt->fetch();
        
        if ($student) {
            echo "<p>✅ Found test student: " . htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) . "</p>";
            
            if (empty($student['phone']) && empty($student['guardian_phone'])) {
                echo "<p>❌ Student has no phone numbers registered!</p>";
            } else {
                echo "<p>✅ Student phone: " . htmlspecialchars($student['phone'] ?? 'N/A') . "</p>";
                echo "<p>✅ Guardian phone: " . htmlspecialchars($student['guardian_phone'] ?? 'N/A') . "</p>";
                
                // Test the SMS template
                $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_tpl_fee_payment'");
                $stmt->execute();
                $template_result = $stmt->fetch();
                
                if ($template_result) {
                    echo "<p>✅ Fee payment template found: " . htmlspecialchars(substr($template_result['setting_value'], 0, 50)) . "...</p>";
                } else {
                    echo "<p>❌ Fee payment template not found!</p>";
                }
                
                echo "<h3>Simulating SMS Send:</h3>";
                echo "<p>This would normally call: sms_send_template(\$pdo, 'fee_payment', \$vars, \$numbers)</p>";
                
            }
        } else {
            echo "<p>❌ Test student not found. Please create a test student first.</p>";
        }
        
    } else {
        echo "<h3 style='color: red;'>❌ Some required settings are not enabled!</h3>";
        echo "<p>Please go to the admin panel and enable all required SMS settings.</p>";
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}

echo "<h3>Next Steps:</h3>";
echo "<ol>";
echo "<li>Verify all settings are enabled in admin panel</li>";
echo "<li>Check if students have phone numbers registered</li>";
echo "<li>Test with a real fee collection</li>";
echo "<li>Check SMS logs for any errors</li>";
echo "</ol>";
?>