<?php
// =========================================================================
// api/bot_api.php - LEGACY PROXY → REDIRECT KE ENGINE UTAMA
// =========================================================================
// File ini adalah endpoint LAMA yang sebelumnya menembak API langsung
// TANPA cache dan TANPA 16-slot router.
//
// SEKARANG: Semua traffic di-proxy ke engine utama (llm/api/bot_api.php)
// agar mendapat benefit:
//   ✅ Xieqa Cache Engine (Fuzzy Cache)
//   ✅ 16-Slot Auto-Router (Load Balancing)
//   ✅ Anti-DDoS & Rate Limiting
//   ✅ SaaS Billing Terukur
//
// Developer: Sikawarsito, S.Kom
// =========================================================================

error_reporting(0);
ini_set('display_errors', 0);

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header("Content-Type: application/json; charset=UTF-8");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(["status" => "error", "message" => "Method Not Allowed. Use POST."]);
    exit;
}

// =========================================================================
// PROXY: Teruskan request UTUH ke engine utama (llm/api/bot_api.php)
// =========================================================================
$scheme   = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host     = $_SERVER['HTTP_HOST'];
$doc_root = rtrim(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']), '/');
$engine   = str_replace('\\', '/', realpath(__DIR__ . '/../llm/api/bot_api.php'));
$web_path = str_replace($doc_root, '', $engine);
$engine_url = $scheme . '://' . $host . $web_path;

// Ambil body dan header asli dari request yang masuk
$raw_body = file_get_contents('php://input');

$headers_forward = ['Content-Type: application/json'];

// Teruskan Authorization header apa adanya
$auth_header = '';
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
    $auth_header = $_SERVER['HTTP_AUTHORIZATION'];
} elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
    $auth_header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
} elseif (function_exists('apache_request_headers')) {
    $reqHeaders = apache_request_headers();
    if (isset($reqHeaders['Authorization'])) {
        $auth_header = $reqHeaders['Authorization'];
    }
}

if (!empty($auth_header)) {
    $headers_forward[] = "Authorization: " . $auth_header;
}

// Teruskan header internal bypass jika ada
if (isset($_SERVER['HTTP_X_XIEQA_INTERNAL'])) {
    $headers_forward[] = "X-Xieqa-Internal: " . $_SERVER['HTTP_X_XIEQA_INTERNAL'];
}

// Eksekusi proxy
$ch = curl_init($engine_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_forward);
curl_setopt($ch, CURLOPT_POSTFIELDS, $raw_body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);

$response  = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Kembalikan response dari engine utama apa adanya
http_response_code($http_code);
echo $response;
?>