<?php
// Simplified version without session requirements
require_once '../config/database.php';
require_once '../includes/sms_helper.php';

// Load current non-masking configuration
$config = non_musking_config();

// MRAM Portal Configuration for comparison
$mram_config = [
    'username' => 'C3002791',
    'password' => 'hPOWSnw8rf',
    'api_key' => 'C30027916974e980709412.23354057',
    'sender_id' => '8809601016579',
    'api_url' => 'https://rest.mrmessaging.net/messages',
];

echo "<h1>নন-মাস্কিং SMS টেস্ট</h1>";

echo "<h2>Configuration Comparison:</h2>";
echo "<table border='1' cellpadding='5'>";
echo "<tr><th>Setting</th><th>MRAM Portal</th><th>Local System</th><th>Status</th></tr>";
echo "<tr><td>Enabled</td><td>Yes</td><td>" . ($config['enabled'] ? 'Yes' : 'No') . "</td><td>" . ((bool)$config['enabled'] === true ? '<span style=\"color:green;\">✓ OK</span>' : '<span style=\"color:red;\">✗ Disabled</span>') . "</td></tr>";
echo "<tr><td>API URL</td><td>{$mram_config['api_url']}</td><td>" . ($config['api_url'] ?? 'Not set') . "</td><td>" . ($config['api_url'] === $mram_config['api_url'] ? '<span style=\"color:green;\">✓ OK</span>' : '<span style=\"color:red;\">✗ Mismatch</span>') . "</td></tr>";
echo "<tr><td>Username</td><td>{$mram_config['username']}</td><td>" . ($config['username'] ?? 'Not set') . "</td><td>" . ($config['username'] === $mram_config['username'] ? '<span style=\"color:green;\">✓ OK</span>' : '<span style=\"color:red;\">✗ Mismatch</span>') . "</td></tr>";
echo "<tr><td>API Key</td><td>" . substr($mram_config['api_key'], 0, 10) . '...' . "</td><td>" . (!empty($config['api_key']) ? substr($config['api_key'], 0, 10) . '...' : 'Not set') . "</td><td>" . ($config['api_key'] === $mram_config['api_key'] ? '<span style=\"color:green;\">✓ OK</span>' : '<span style=\"color:red;\">✗ Mismatch</span>') . "</td></tr>";
echo "<tr><td>Sender ID</td><td>{$mram_config['sender_id']}</td><td>" . ($config['sender_id'] ?? 'Not set') . "</td><td>" . ($config['sender_id'] === $mram_config['sender_id'] ? '<span style=\"color:green;\">✓ OK</span>' : '<span style=\"color:red;\">✗ Mismatch</span>') . "</td></tr>";
echo "</table>";

echo "<h2>Send Test SMS:</h2>";
echo "<form method='POST'>";
echo "<p>Phone: <input type='tel' name='phone' placeholder='01712345678' required></p>";
echo "<p>Message: <textarea name='message' required>Test message from Bidda system</textarea></p>";
echo "<p><input type='submit' name='send' value='Send Test SMS'></p>";
echo "</form>";

if (isset($_POST['send'])) {
    $phone = trim($_POST['phone']);
    $message = trim($_POST['message']);
    
    if (!empty($phone) && !empty($message)) {
        // Ensure phone number is in international format
        if (substr($phone, 0, 1) === '0') {
            $phone = '+88' . $phone;
        } elseif (substr($phone, 0, 2) === '88') {
            $phone = '+' . $phone;
        } elseif (substr($phone, 0, 3) === '+88') {
            $phone = $phone; // Already correct
        } else {
            $phone = '+88' . $phone;
        }
        
        echo "<h3>Sending test SMS to $phone...</h3>";
        $result = non_musking_send($pdo, [$phone], $message, ['test' => true]);
        echo "<pre>" . print_r($result, true) . "</pre>";
    }
}
?>