<?php
require_once '../includes/sms_notification_system.php';

echo "<h2>Automated SMS Notification System Test</h2>";

// Database connection
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]);
    
    $smsSystem = new SmsNotificationSystem($pdo);
    
    echo "<h3>System Status: ✅ Ready</h3>";
    
    // Check if SMS notifications are enabled
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_notification_enabled'");
    $stmt->execute();
    $enabled = $stmt->fetch();
    
    if ($enabled && $enabled['setting_value'] === '1') {
        echo "<p>✅ SMS Notifications: <strong>ENABLED</strong></p>";
    } else {
        echo "<p>⚠️ SMS Notifications: <strong>DISABLED</strong> - Enable in admin panel</p>";
    }
    
    // Check if application notifications are enabled
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_application_confirmation'");
    $stmt->execute();
    $app_enabled = $stmt->fetch();
    
    if ($app_enabled && $app_enabled['setting_value'] === '1') {
        echo "<p>✅ Application Notifications: <strong>ENABLED</strong></p>";
    } else {
        echo "<p>⚠️ Application Notifications: <strong>DISABLED</strong> - Enable in admin panel</p>";
    }
    
    echo "<h3>Test SMS Sending:</h3>";
    
    // Form for testing
    echo "<form method='post'>";
    echo "<div class='mb-3'>";
    echo "<label for='phone' class='form-label'>Phone Number:</label>";
    echo "<input type='text' class='form-control' id='phone' name='phone' value='+8801977861762' required>";
    echo "</div>";
    
    echo "<div class='mb-3'>";
    echo "<label for='test_type' class='form-label'>Test Type:</label>";
    echo "<select class='form-select' id='test_type' name='test_type' required>";
    echo "<option value='application'>Application Confirmation</option>";
    echo "<option value='fee'>Fee Notification</option>";
    echo "<option value='result'>Result Notification</option>";
    echo "</select>";
    echo "</div>";
    
    echo "<div class='mb-3'>";
    echo "<label for='student_name' class='form-label'>Student Name:</label>";
    echo "<input type='text' class='form-control' id='student_name' name='student_name' value='John Doe' required>";
    echo "</div>";
    
    echo "<div class='mb-3' id='application_fields'>";
    echo "<label for='application_no' class='form-label'>Application Number:</label>";
    echo "<input type='text' class='form-control' id='application_no' name='application_no' value='APP-2024-000001'>";
    echo "</div>";
    
    echo "<div class='mb-3 d-none' id='fee_fields'>";
    echo "<label for='amount' class='form-label'>Amount:</label>";
    echo "<input type='text' class='form-control' id='amount' name='amount' value='5000'>";
    echo "<label for='due_date' class='form-label'>Due Date:</label>";
    echo "<input type='date' class='form-control' id='due_date' name='due_date' value='".date('Y-m-d')."'>";
    echo "</div>";
    
    echo "<div class='mb-3 d-none' id='result_fields'>";
    echo "<label for='grade' class='form-label'>Grade:</label>";
    echo "<input type='text' class='form-control' id='grade' name='grade' value='A+''>";
    echo "<label for='gpa' class='form-label'>GPA:</label>";
    echo "<input type='text' class='form-control' id='gpa' name='gpa' value='5.00'>";
    echo "</div>";
    
    echo "<button type='submit' class='btn btn-primary'>Send Test SMS</button>";
    echo "</form>";
    
    if ($_POST) {
        $phone = $_POST['phone'];
        $test_type = $_POST['test_type'];
        $student_name = $_POST['student_name'];
        
        echo "<h4>Sending Test SMS...</h4>";
        
        $result = null;
        
        switch ($test_type) {
            case 'application':
                $application_no = $_POST['application_no'];
                $result = $smsSystem->sendApplicationNotification(1, $student_name, $application_no, $phone);
                break;
                
            case 'fee':
                $amount = $_POST['amount'];
                $due_date = $_POST['due_date'];
                $result = $smsSystem->sendFeeNotification(1, $student_name, $amount, $due_date, $phone);
                break;
                
            case 'result':
                $grade = $_POST['grade'];
                $gpa = $_POST['gpa'];
                $result = $smsSystem->sendResultNotification(1, $student_name, $grade, $gpa, $phone);
                break;
        }
        
        if ($result) {
            echo "<h5>Response:</h5>";
            echo "<p><strong>Success:</strong> " . ($result['success'] ? 'Yes' : 'No') . "</p>";
            echo "<p><strong>Sent Count:</strong> " . $result['sent_count'] . "</p>";
            echo "<p><strong>Failed Count:</strong> " . $result['failed_count'] . "</p>";
            echo "<p><strong>HTTP Code:</strong> " . ($result['http_code'] ?? 'N/A') . "</p>";
            echo "<p><strong>Raw Response:</strong> " . htmlspecialchars($result['raw_response']) . "</p>";
            
            if ($result['success']) {
                echo "<p style='color:green; font-weight:bold;'>✅ SMS Sent Successfully!</p>";
            } else {
                echo "<p style='color:red; font-weight:bold;'>❌ SMS Failed to Send</p>";
            }
        } else {
            echo "<p style='color:red;'>❌ Error sending SMS</p>";
        }
    }
    
    echo "<script>
    document.getElementById('test_type').addEventListener('change', function() {
        var type = this.value;
        document.getElementById('application_fields').className = 'mb-3 d-none';
        document.getElementById('fee_fields').className = 'mb-3 d-none';
        document.getElementById('result_fields').className = 'mb-3 d-none';
        
        if (type === 'application') {
            document.getElementById('application_fields').className = 'mb-3';
        } else if (type === 'fee') {
            document.getElementById('fee_fields').className = 'mb-3';
        } else if (type === 'result') {
            document.getElementById('result_fields').className = 'mb-3';
        }
    });
    </script>";
    
} catch (Exception $e) {
    echo "<p style='color:red;'>Error: " . $e->getMessage() . "</p>";
}
?>