<?php
// Debug script to check current SMS settings
require_once 'config/database.php';

echo "<h3>Current SMS Settings in Database</h3>";

try {
    // Query 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();
    $results = $stmt->fetchAll();

    if (count($results) > 0) {
        echo "<table border='1'>";
        echo "<tr><th>Setting Key</th><th>Setting Value</th></tr>";
        
        foreach ($results as $row) {
            $key = htmlspecialchars($row['setting_key']);
            $value = htmlspecialchars($row['setting_value']);
            echo "<tr><td>{$key}</td><td>{$value}</td></tr>";
        }
        echo "</table>";
    } else {
        echo "<p>No SMS settings found in database</p>";
    }

    // Check specifically for bank transaction settings
    echo "<h3>Bank Transaction SMS Settings</h3>";
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_bank_transaction_notifications'");
    $stmt->execute();
    $result = $stmt->fetch();

    if ($result) {
        $value = $result['setting_value'];
        if ($value === '1') {
            echo "<p>✅ Bank transaction notifications are ENABLED</p>";
        } else {
            echo "<p>❌ Bank transaction notifications are DISABLED (value: {$value})</p>";
        }
    } else {
        echo "<p>❌ Bank transaction notifications setting NOT FOUND</p>";
    }

    // Check non-musking enabled
    echo "<h3>Non-Musking SMS Service</h3>";
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_enabled'");
    $stmt->execute();
    $result = $stmt->fetch();

    if ($result) {
        $value = $result['setting_value'];
        if ($value === '1') {
            echo "<p>✅ Non-musking SMS service is ENABLED</p>";
        } else {
            echo "<p>❌ Non-musking SMS service is DISABLED (value: {$value})</p>";
        }
    } else {
        echo "<p>❌ Non-musking SMS service setting NOT FOUND</p>";
    }

    // Check API credentials
    echo "<h3>Non-Musking API Credentials</h3>";
    $credentials = [
        'non_musking_api_key',
        'non_musking_api_url',
        'non_musking_sender_id'
    ];

    foreach ($credentials as $cred) {
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
        $stmt->execute([$cred]);
        $result = $stmt->fetch();
        
        if ($result && !empty($result['setting_value'])) {
            echo "<p>✅ {$cred}: SET</p>";
        } else {
            echo "<p>❌ {$cred}: NOT SET</p>";
        }
    }

    // Check if students have phone numbers
    echo "<h3>Student Phone Numbers Check</h3>";
    $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM students WHERE phone IS NOT NULL AND phone != ''");
    $stmt->execute();
    $result = $stmt->fetch();
    
    $phone_count = $result['count'];
    echo "<p>Students with phone numbers: {$phone_count}</p>";

    // Check recent bank transactions
    echo "<h3>Recent Bank Transactions</h3>";
    $stmt = $pdo->prepare("
        SELECT bt.*, s.first_name, s.last_name, s.phone, s.guardian_phone
        FROM bank_transactions bt
        JOIN students s ON bt.student_id = s.id
        ORDER BY bt.submitted_at DESC
        LIMIT 5
    ");
    $stmt->execute();
    $transactions = $stmt->fetchAll();
    
    if (count($transactions) > 0) {
        echo "<table border='1'>";
        echo "<tr><th>ID</th><th>Student</th><th>Amount</th><th>Status</th><th>Student Phone</th><th>Guardian Phone</th></tr>";
        foreach ($transactions as $tx) {
            echo "<tr>";
            echo "<td>{$tx['id']}</td>";
            echo "<td>{$tx['first_name']} {$tx['last_name']}</td>";
            echo "<td>{$tx['amount']}</td>";
            echo "<td>{$tx['status']}</td>";
            echo "<td>{$tx['phone']}</td>";
            echo "<td>{$tx['guardian_phone']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p>No bank transactions found</p>";
    }

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