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

echo "Detailed Zashof API Test:\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 "API Key: " . (strlen($api_key) > 20 ? substr($api_key, 0, 10) . '...' . substr($api_key, -10) : $api_key) . "\n";
    echo "Base URL: " . $base_url . "\n";
    echo "Create Charge URL: " . $create_charge_url . "\n";
    
    // Test different ways of sending the API key
    $test_methods = [
        'Bearer Token' => 'Bearer ' . $api_key,
        'Token Only' => $api_key,
        'Bearer with space' => 'Bearer  ' . $api_key,
    ];
    
    $data = [
        'amount' => 100,
        'currency' => 'BDT',
        'redirect_url' => 'http://localhost/fees/zashof_success.php',
        'cancel_url' => 'http://localhost/fees/my_fees.php',
        'client_name' => 'Test Student',
        'client_email' => 'test@example.com',
        'client_mobile' => '01712345678',
        'student_id' => '12345',
        'description' => 'Test Payment',
        'metadata' => [
            'student_id' => '12345',
            'student_name' => 'Test Student',
            'payment_for' => 'test'
        ]
    ];
    
    foreach ($test_methods as $method_name => $auth_header) {
        echo "\n--- Testing $method_name ---\n";
        
        $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: ' . $auth_header,
            'Accept: application/json'
        ]);
        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 "HTTP Response Code: " . $httpCode . "\n";
        if ($error) {
            echo "cURL Error: " . $error . "\n";
        }
        
        $response_data = json_decode($response, true);
        if ($response_data && isset($response_data['message'])) {
            echo "API Message: " . $response_data['message'] . "\n";
        } else {
            echo "Response: " . $response . "\n";
        }
        
        if ($httpCode === 200 || $httpCode === 201) {
            echo "SUCCESS with $method_name\n";
            break;
        }
    }
    
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage() . "\n";
}
?>