<?php
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/includes/zashof.php';

echo "Testing Zashof API Connection...\n";

try {
    $zashof = new ZashofPayment();
    
    if (!$zashof->isEnabled()) {
        throw new Exception("Zashof payment gateway is not enabled or properly configured");
    }
    
    // Test the base URL connection
    echo "Testing Base URL Connection...\n";
    
    $reflection = new ReflectionClass($zashof);
    $baseUrlProperty = $reflection->getProperty('base_url');
    $baseUrlProperty->setAccessible(true);
    $base_url = $baseUrlProperty->getValue($zashof);
    
    echo "- Base URL: " . $base_url . "\n";
    
    // Test connection to 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 Response Code: " . $httpCode . "\n";
    if ($error) {
        echo "- cURL Error: " . $error . "\n";
    } else {
        echo "- Connection: Successful\n";
    }
    
    // Test create charge URL
    $createChargeUrlProperty = $reflection->getProperty('create_charge_url');
    $createChargeUrlProperty->setAccessible(true);
    $create_charge_url = $createChargeUrlProperty->getValue($zashof);
    
    echo "\nTesting Create Charge URL...\n";
    echo "- URL: " . $create_charge_url . "\n";
    
    // Prepare a simple test request
    $apiKeyProperty = $reflection->getProperty('api_key');
    $apiKeyProperty->setAccessible(true);
    $api_key = $apiKeyProperty->getValue($zashof);
    
    $data = [
        'amount' => 100,
        'currency' => 'BDT'
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $create_charge_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key,
        'Accept: application/json'
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    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 Response Code: " . $httpCode . "\n";
    if ($error) {
        echo "- cURL Error: " . $error . "\n";
    }
    
    if ($httpCode === 401) {
        echo "- ERROR: Unauthorized request. Invalid API key.\n";
        echo "  This indicates the API key is not valid or has been revoked.\n";
        echo "  Please check your Zashof API key in the admin settings.\n";
    } elseif ($httpCode === 400) {
        echo "- INFO: Bad Request. This is expected since we didn't send all required data.\n";
        echo "  But it means the API key authentication was successful.\n";
    } elseif ($httpCode === 200 || $httpCode === 201) {
        echo "- SUCCESS: API key is valid and working.\n";
    } else {
        echo "- Response: " . substr($response, 0, 200) . "...\n";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>