<?php
/**
 * api_diag.php
 * Standalone Kindamba Pay API Diagnostic Tool
 */
header('Content-Type: text/html; charset=utf-8');
require_once 'config.php';
require_once 'includes/kindamba.php';

echo "<html><head><title>Kindamba API Diagnostics</title>";
echo "<style>body{font-family:sans-serif;line-height:1.5;padding:20px;max-width:800px;margin:0 auto;background:#f4f7f6;} h1{color:#2c3e50;} .card{background:#fff;padding:20px;border-radius:8px;box-shadow:0 2px 4px rgba(0,0,0,0.1);margin-bottom:20px;} .success{color:#27ae60;font-weight:bold;} .error{color:#e74c3c;font-weight:bold;} pre{background:#eee;padding:10px;border-radius:4px;overflow-x:auto;}</style>";
echo "</head><body>";

echo "<h1>Kindamba Pay API Diagnostics</h1>";

// 1. Check Configuration
echo "<div class='card'><h2>1. Configuration Check</h2>";
echo "<ul>";
echo "<li><b>API URL:</b> " . KINDAMBA_API_URL . "</li>";
echo "<li><b>Public Key:</b> " . (defined('KINDAMBA_PUBLIC_KEY') ? substr(KINDAMBA_PUBLIC_KEY, 0, 10) . "..." : "<span class='error'>NOT DEFINED</span>") . "</li>";
echo "<li><b>SITE_URL:</b> " . SITE_URL . "</li>";
echo "</ul></div>";

// 2. Server Environment
echo "<div class='card'><h2>2. Server Environment</h2>";
echo "<ul>";
echo "<li><b>PHP Version:</b> " . PHP_VERSION . "</li>";
echo "<li><b>cURL Enabled:</b> " . (function_exists('curl_init') ? "<span class='success'>Yes</span>" : "<span class='error'>No (CRITICAL)</span>") . "</li>";
if (function_exists('curl_version')) {
    $version = curl_version();
    echo "<li><b>cURL Version:</b> " . $version['version'] . "</li>";
    echo "<li><b>SSL Version:</b> " . $version['ssl_version'] . "</li>";
}
echo "</ul></div>";

// 3. Connectivity Test
echo "<div class='card'><h2>3. Connectivity Test (Status Check)</h2>";
$testId = 'diag-' . time();
echo "<p>Attempting to reach Kindamba API with a dummy status check...</p>";

$start = microtime(true);
$result = kindamba_verify_status($testId);
$end = microtime(true);
$duration = round(($end - $start) * 1000, 2);

echo "<p>Time taken: $duration ms</p>";
echo "<p>HTTP Response Code: " . $result['code'] . "</p>";

if ($result['code'] > 0) {
    if ($result['code'] === 401) {
        echo "<p class='error'>❌ Authentication Failed: The API key is rejected by Kindamba (401 Unauthorized).</p>";
    } elseif ($result['code'] === 200 || $result['code'] === 404) {
        echo "<p class='success'>✅ API Reachable! (Received " . $result['code'] . " from server).</p>";
    } else {
        echo "<p class='error'>❌ Unexpected Response: Server returned code " . $result['code'] . ".</p>";
    }
} else {
    echo "<p class='error'>❌ CONNECTION FAILED: Could not reach the API server.</p>";
}

echo "<h3>Raw Data:</h3>";
echo "<pre>" . htmlspecialchars(print_r($result['data'], true)) . "</pre>";
echo "</div>";

// 4. DNS Check
echo "<div class='card'><h2>4. DNS & Network Check</h2>";
$host = parse_url(KINDAMBA_API_URL, PHP_URL_HOST);
echo "<p>Resolving host: <b>$host</b></p>";
$ip = gethostbyname($host);
if ($ip !== $host) {
    echo "<p class='success'>✅ Resolved to: $ip</p>";
} else {
    echo "<p class='error'>❌ DNS Resolution Failed. Your server cannot find the Kindamba API domain.</p>";
}
echo "</div>";

echo "</body></html>";
