<?php
// Test UddoktaPay connectivity and configuration
require_once 'config/database.php';
require_once 'config/uddoktapay.php';
require_once 'includes/uddoktapay.php';

echo "UddoktaPay Connectivity and Configuration Test\n";
echo "============================================\n\n";

// Test 1: Configuration check
echo "1. Configuration Check:\n";
$config = get_uddoktapay_config();
echo "   Enabled: " . ($config['enabled'] ? 'Yes' : 'No') . "\n";
echo "   API Key Set: " . (!empty($config['api_key']) ? 'Yes' : 'No') . "\n";
echo "   Base URL: " . $config['base_url'] . "\n";
echo "   Sandbox Mode: " . ($config['sandbox_mode'] ? 'Yes' : 'No') . "\n";
echo "   Redirect URL: " . $config['redirect_url'] . "\n";
echo "   Cancel URL: " . $config['cancel_url'] . "\n\n";

// Test 2: Configuration validation
echo "2. Configuration Validation:\n";
$validation = validate_uddoktapay_config();
echo "   Valid: " . ($validation['success'] ? 'Yes' : 'No') . "\n";
echo "   Message: " . $validation['message'] . "\n\n";

// Test 3: API connectivity
echo "3. API Connectivity Test:\n";
$uddoktapay = new UddoktaPay();
$enabled = $uddoktapay->isEnabled();
echo "   Gateway Enabled: " . ($enabled ? 'Yes' : 'No') . "\n";

if ($enabled) {
    echo "   Testing API endpoint connectivity...\n";
    
    // Test the base URL
    $base_url = $config['base_url'];
    echo "   Base URL: $base_url\n";
    
    // Try to connect to the base URL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $base_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);
    
    curl_close($ch);
    
    echo "   HTTP Status Code: $httpCode\n";
    if ($error) {
        echo "   cURL Error: $error\n";
    } else {
        echo "   Connection: Successful\n";
        $preview = substr($response, 0, 100);
        echo "   Response Preview: $preview...\n";
    }
} else {
    echo "   Skipping connectivity test - gateway not enabled\n";
}

echo "\n4. Test Invoice Verification (Optional):\n";
echo "   To test invoice verification, provide an invoice ID:\n";
echo "   Usage: php test_uddoktapay_connectivity.php <invoice_id>\n";

// If an invoice ID is provided as command line argument, test verification
if (isset($argv[1])) {
    $invoice_id = $argv[1];
    echo "   Testing verification for invoice ID: $invoice_id\n";
    
    $result = $uddoktapay->verifyPayment($invoice_id);
    echo "   Verification Success: " . ($result['success'] ? 'Yes' : 'No') . "\n";
    echo "   Message: " . $result['message'] . "\n";
    
    if (isset($result['status'])) {
        echo "   Status: " . $result['status'] . "\n";
    }
    
    if (isset($result['full_response'])) {
        echo "   Full Response: " . json_encode($result['full_response'], JSON_PRETTY_PRINT) . "\n";
    }
} else {
    echo "   No invoice ID provided - skipping verification test\n";
}

echo "\nTest completed.\n";
?>