<?php
require_once 'config/database.php';
require_once 'includes/sms_helper.php';

echo "<h2>MRAM Authentication Troubleshooter</h2>";

// Get current configuration
$config = non_musking_config();

echo "<h3>Current Configuration:</h3>";
echo "<ul>";
echo "<li>Enabled: " . ($config['enabled'] ? 'Yes' : 'No') . "</li>";
echo "<li>API URL: " . htmlspecialchars($config['api_url'] ?? 'Not set') . "</li>";
echo "<li>Username: " . (!empty($config['username']) ? htmlspecialchars($config['username']) : 'Not set') . "</li>";
echo "<li>API Key: " . (!empty($config['api_key']) ? substr(htmlspecialchars($config['api_key']), 0, 10) . '...' : 'Not set') . "</li>";
echo "<li>Sender ID: " . htmlspecialchars($config['sender_id'] ?? 'Not set') . "</li>";
echo "</ul>";

// Test API connectivity with correct format
if (!empty($_GET['test']) && $_GET['test'] == 'direct') {
    echo "<h3>Direct API Test</h3>";
    
    // Prepare the payload as required by MRAM
    $payload = [
        'mobile_number' => '+8801712345678',
        'sms' => 'Test message from Bidda system',
        'sender_id' => $config['sender_id'] ?? 'BiddaSchool'
    ];
    
    // Create cURL request manually to test
    $ch = curl_init('https://rest.mrmessaging.net/messages');
    
    // Try with API key first
    $authHeader = '';
    if (!empty($config['api_key'])) {
        $authHeader = 'Authorization: App ' . $config['api_key'];
        echo "<p>Using API Key authentication</p>";
    } else {
        // Fallback to Basic Auth
        $username = $config['username'] ?? '';
        $password = $config['password'] ?? '';
        $authHeader = 'Authorization: Basic ' . base64_encode($username . ':' . $password);
        echo "<p>Using Basic authentication</p>";
    }
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Accept: application/json',
        $authHeader
    ]);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlError = curl_error($ch);
    curl_close($ch);
    
    echo "<p><strong>HTTP Code:</strong> $httpCode</p>";
    echo "<p><strong>Response:</strong></p><pre>" . htmlspecialchars($response) . "</pre>";
    
    if ($curlError) {
        echo "<p><strong>cURL Error:</strong> " . htmlspecialchars($curlError) . "</p>";
    }
    
    if ($httpCode == 401) {
        echo "<h3>Authentication Issue Detected</h3>";
        echo "<p>401 error means your credentials are not accepted. Please verify:</p>";
        echo "<ol>";
        echo "<li>Your API key is exactly: <strong>C30027916974e980709412.23354057</strong></li>";
        echo "<li>Your username is: <strong>C3002791</strong></li>";
        echo "<li>Your password is: <strong>hPOWSnw8rf</strong></li>";
        echo "<li>Your account has API access enabled</li>";
        echo "<li>You have sufficient balance in your account</li>";
        echo "</ol>";
    }
}

echo "<h3>Troubleshooting Options:</h3>";
echo "<ul>";
echo "<li><a href='?test=direct'>Direct API Test</a> - Test the API call directly</li>";
echo "</ul>";

echo "<h3>Common Issues:</h3>";
echo "<ol>";
echo "<li><strong>Wrong API Key</strong>: Make sure the API key is exactly 'C30027916974e980709412.23354057'</li>";
echo "<li><strong>Account not activated</strong>: Your MRAM account might not have API access enabled</li>";
echo "<li><strong>Insufficient balance</strong>: Check if your account has enough balance</li>";
echo "<li><strong>Incorrect authorization header</strong>: MRAM requires 'Authorization: App [API_KEY]'</li>";
echo "</ol>";

?>