<?php
/**
 * Quick SMS Test for Bank Transaction Verification
 * Simple test to verify SMS functionality works
 */

// This script can be included directly in verify_bank_transactions.php for testing

if (basename($_SERVER['PHP_SELF']) !== 'quick_sms_test.php') {
    // If included from another script, don't run automatically
    return;
}

header('Content-Type: text/html; charset=utf-8');

echo "<!DOCTYPE html>
<html>
<head>
    <title>Quick SMS Test</title>
    <meta charset='UTF-8'>
    <style>
        body { font-family: Arial, sans-serif; max-width: 600px; margin: 20px auto; padding: 20px; }
        .result { padding: 15px; margin: 10px 0; border-radius: 5px; }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .info { background: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
    </style>
</head>
<body>
    <h2>⚡ Quick SMS Test</h2>";

try {
    require_once 'config/database.php';
    
    echo "<div class='info'>✅ Database connected</div>";
    
    // Test 1: Check critical settings
    echo "<h3>Test 1: Critical Settings Check</h3>";
    
    $critical_settings = [
        'sms_enabled' => 'SMS Service',
        'non_musking_enabled' => 'Non-Musking SMS',
        'sms_send_bank_transaction_notifications' => 'Bank Transaction Notifications'
    ];
    
    $all_enabled = true;
    
    foreach ($critical_settings as $key => $description) {
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
        $stmt->execute([$key]);
        $result = $stmt->fetch();
        
        if ($result && $result['setting_value'] === '1') {
            echo "<div class='success'>✅ $description: ENABLED</div>";
        } else {
            echo "<div class='error'>❌ $description: DISABLED</div>";
            $all_enabled = false;
        }
    }
    
    // Test 2: Check student with phone
    echo "<h3>Test 2: Student Phone Check</h3>";
    
    $stmt = $pdo->prepare("
        SELECT s.id, s.first_name, s.last_name, s.phone 
        FROM students s 
        WHERE s.phone IS NOT NULL AND s.phone != '' 
        LIMIT 1
    ");
    $stmt->execute();
    $student = $stmt->fetch();
    
    if ($student) {
        echo "<div class='success'>✅ Found student with phone: " . htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) . " (" . $student['phone'] . ")</div>";
    } else {
        echo "<div class='error'>❌ No students with phone numbers found</div>";
        $all_enabled = false;
    }
    
    // Test 3: SMS functionality test
    echo "<h3>Test 3: SMS Functionality Test</h3>";
    
    if ($all_enabled && $student) {
        $smsHelperPath = __DIR__ . '/includes/sms_helper.php';
        if (file_exists($smsHelperPath)) {
            require_once $smsHelperPath;
            
            try {
                $smsSystem = new SmsNotificationSystem($pdo);
                
                $result = $smsSystem->sendBankTransactionNotification(
                    $student['id'],
                    trim($student['first_name'] . ' ' . $student['last_name']),
                    1000, // test amount
                    500,  // test due amount
                    $student['phone']
                );
                
                if ($result['success']) {
                    echo "<div class='success'>🎉 <strong>SUCCESS!</strong> SMS sent to " . $student['phone'] . "</div>";
                    echo "<p>Message: " . htmlspecialchars($result['message']) . "</p>";
                } else {
                    echo "<div class='error'>❌ SMS failed: " . htmlspecialchars($result['message'] ?? 'Unknown error') . "</div>";
                }
                
            } catch (Exception $e) {
                echo "<div class='error'>❌ SMS test failed: " . $e->getMessage() . "</div>";
            }
        } else {
            echo "<div class='error'>❌ SMS helper not found</div>";
        }
    } else {
        echo "<div class='error'>❌ Cannot test SMS - missing prerequisites</div>";
    }
    
    // Final result
    echo "<h3>📋 Test Results Summary</h3>";
    
    if ($all_enabled && $student) {
        echo "<div class='success' style='font-size: 18px;'>";
        echo "✅ <strong>ALL TESTS PASSED!</strong>";
        echo "<p>Bank transaction SMS notifications should work properly.</p>";
        echo "</div>";
    } else {
        echo "<div class='error'>";
        echo "<strong>Issues Found:</strong>";
        echo "<ul>";
        if (!$all_enabled) {
            echo "<li>Required SMS settings are not enabled</li>";
        }
        if (!$student) {
            echo "<li>No students with phone numbers</li>";
        }
        echo "</ul>";
        echo "</div>";
    }
    
} catch (Exception $e) {
    echo "<div class='error'>";
    echo "<strong>Error:</strong> " . $e->getMessage();
    echo "</div>";
}

echo "<div style='margin-top: 20px;'>";
echo "<a href='verify_bank_transactions.php' style='background: #007cba; color: white; padding: 10px 15px; text-decoration: none; border-radius: 5px;'>← Back to Verification Page</a>";
echo "</div>";

echo "</body>
</html>";
?>