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

echo "Testing Zashof API Endpoint:\n";
echo "===========================\n";

try {
    // Get the Zashof configuration directly from database
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'zashof_%'");
    $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
    
    $api_key = $settings['zashof_api_key'] ?? '';
    $base_url = $settings['zashof_base_url'] ?? '';
    $create_charge_url = $settings['zashof_create_charge_url'] ?? '';
    
    echo "Base URL: " . $base_url . "\n";
    echo "Create Charge URL: " . $create_charge_url . "\n";
    
    // First, let's test if the base URL is accessible
    echo "\n--- Testing Base URL Connectivity ---\n";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $base_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Bidda School Management System/1.0');
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);
    curl_close($ch);
    
    echo "Base URL HTTP Response Code: " . $httpCode . "\n";
    if ($error) {
        echo "cURL Error: " . $error . "\n";
    }
    
    // Now let's test the create charge endpoint with a simple GET request
    echo "\n--- Testing Create Charge Endpoint ---\n";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $create_charge_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Bidda School Management System/1.0');
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);
    curl_close($ch);
    
    echo "Create Charge Endpoint HTTP Response Code: " . $httpCode . "\n";
    if ($error) {
        echo "cURL Error: " . $error . "\n";
    }
    echo "Response: " . $response . "\n";
    
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage() . "\n";
}
?>