<?php
/**
 * Test Non-Masking SMS Functionality
 * 
 * This script tests the non-masking SMS functionality
 */

require_once(__DIR__ . '/config/database.php');
require_once(__DIR__ . '/includes/sms_helper.php');

echo "<h2>Testing Non-Masking SMS Functionality</h2>\n";

// Check if non-masking is enabled
$cfg = non_musking_config();
echo "<h3>Configuration Status:</h3>\n";
echo "Enabled: " . ($cfg['enabled'] ? 'Yes' : 'No') . "<br>\n";
echo "Method: " . $cfg['method'] . "<br>\n";
echo "API URL: " . ($cfg['api_url'] ? $cfg['api_url'] : 'Not set') . "<br>\n";
echo "Sender ID: " . $cfg['sender_id'] . "<br>\n";

if (!$cfg['enabled']) {
    echo "<p style='color: red;'>⚠️ Non-masking SMS is not enabled. Please enable it in the admin panel.</p>";
    exit;
}

if (empty($cfg['api_url'])) {
    echo "<p style='color: orange;'>⚠️ API URL is not configured. Please set it in the admin panel.</p>";
    exit;
}

// Test sending to a single number (replace with a test number)
$testNumber = isset($_GET['test_number']) ? $_GET['test_number'] : '01711111111'; // Default test number
$testMessage = "Test non-masking SMS from Bidda system. Time: " . date('Y-m-d H:i:s');

echo "<h3>Test Parameters:</h3>\n";
echo "Test Number: $testNumber<br>\n";
echo "Test Message: $testMessage<br>\n";

echo "<h3>Test Results:</h3>\n";

try {
    // Send test message
    $result = non_musking_send($pdo, [$testNumber], $testMessage, [
        'message_type' => 'test'
    ]);

    if ($result['success']) {
        echo "<p style='color: green;'>✅ Success! Message sent to {$result['sent_count']} recipients.</p>";
        echo "<p>Sent numbers: " . json_encode($result['to']) . "</p>";
    } else {
        echo "<p style='color: red;'>❌ Failed to send message: " . $result['message'] . "</p>";
    }

    if (isset($result['failed_count']) && $result['failed_count'] > 0) {
        echo "<p style='color: orange;'>Failed count: {$result['failed_count']}</p>";
        echo "<p>Failed numbers: " . json_encode($result['failed']) . "</p>";
    }

    if (isset($result['raw_response'])) {
        echo "<h4>Raw API Response:</h4>";
        echo "<pre>" . htmlspecialchars(print_r($result['raw_response'], true)) . "</pre>";
    }

} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Exception occurred: " . $e->getMessage() . "</p>";
}

echo "<h3>Recent Messages Log:</h3>\n";
try {
    $stmt = $pdo->prepare("
        SELECT message_type, message_content, recipient_count, status, method, sent_at 
        FROM messages_log 
        WHERE method = 'non_musking' 
        ORDER BY sent_at DESC 
        LIMIT 5
    ");
    $stmt->execute();
    $recentMessages = $stmt->fetchAll();
    
    if (!empty($recentMessages)) {
        echo "<table border='1' cellpadding='5' cellspacing='0'>\n";
        echo "<tr><th>Type</th><th>Content</th><th>Recipients</th><th>Status</th><th>Time</th></tr>\n";
        foreach ($recentMessages as $msg) {
            echo "<tr>";
            echo "<td>" . htmlspecialchars($msg['message_type']) . "</td>";
            echo "<td>" . htmlspecialchars(substr($msg['message_content'], 0, 50)) . "...</td>";
            echo "<td>" . $msg['recipient_count'] . "</td>";
            echo "<td>" . $msg['status'] . "</td>";
            echo "<td>" . $msg['sent_at'] . "</td>";
            echo "</tr>\n";
        }
        echo "</table>";
    } else {
        echo "<p>No recent non-masking messages found.</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>Error fetching recent messages: " . $e->getMessage() . "</p>";
}

echo "<h3>Instructions:</h3>\n";
echo "<p>To test with a different number, add ?test_number=01XXXXXXXXX to the URL</p>\n";
echo "<p>Note: For actual testing, replace the test number with a real number you own.</p>\n";
?>