<?php
/**
 * KAJE STORE API PROXY - v3.4
 * Deskripsi: Jembatan komunikasi antara Frontend dan API Kaje Store
 */

// 1. HEADER UNTUK JSON & CORS (Agar bisa diakses dari browser/app)
header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, x-api-key");

// Penanganan preflight request browser
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    exit;
}

// 2. KREDENSIAL API KAJE STORE
$apiKey  = '8eb9026f46a9ebed7c3de2292bd6353fea402c2ae8328f04b728f879a963';
$baseUrl = 'https://end.kaje-store.com';

// 3. MENANGKAP PARAMETER AKSI (api_action)
$action = isset($_GET['api_action']) ? $_GET['api_action'] : '';

// 4. MENANGKAP DATA BODY (JSON Payload)
$rawInput = file_get_contents('php://input');
$inputData = json_decode($rawInput, true) ?: [];

$endpoint = '';

// 5. ROUTING ENDPOINT BERDASARKAN AKSI
switch ($action) {
    // --- KAJE SERVICE ---
    case 'get_products':
        $endpoint = '/api/service/list-product';
        break;

    case 'order_product':
        $endpoint = '/api/service/order-product';
        break;

    // --- XL AUTH ---
    case 'get_otp':
    case 'get-otp':
        $endpoint = '/api/xl-auth/get-otp';
        break;
    case 'login_otp':
    case 'login-otp':
        $endpoint = '/api/xl-auth/login-otp';
        break;
    case 'login_sesi':
    case 'login-sesi':
        $endpoint = '/api/xl-auth/login-sesi';
        break;

    // --- XL INFO ---
    case 'quotas':
        $endpoint = '/api/xl-info/quotas';
        break;
    case 'get_balance':
    case 'balance':
        $endpoint = '/api/xl-info/balance';
        break;
    case 'city':
        $endpoint = '/api/xl-info/city';
        break;
    case 'loans':
        $endpoint = '/api/xl-info/loans';
        break;
    case 'lock_unlock':
        $endpoint = '/api/xl-info/lock-unlock';
        break;

    // --- XL UPDATE (Aksi) ---
    case 'set_lock_balance':
        $endpoint = '/api/xl-update/set-lock-balance';
        break;
    case 'unsubscribe':
        $endpoint = '/api/xl-update/unsubscribe';
        break;

    // --- SERVICE (Produk & Order) ---
    case 'list_package_otp':
        $endpoint = '/api/service/list-package-otp';
        break;
    case 'order_package_otp':
        $endpoint = '/api/service/order-package-otp';
        break;
    case 'list_product':
        $endpoint = '/api/service/list-product';
        break;
    case 'stock_product':
        $endpoint = '/api/service/stock-product';
        break;
    case 'order_product':
        $endpoint = '/api/service/order-product';
        break;
    case 'pre_order':
        $endpoint = '/api/service/pre-order';
        break;
    case 'check_promo':
        $endpoint = '/api/service/check-promo';
        break;

    // --- INFO SISTEM ---
    case 'check_status':
    case 'status':
        $endpoint = '/api/info/trx-id';
        break;
    case 'info_saldo':
    case 'saldo_user':
        $endpoint = '/api/info/saldo';
        break;

    default:
        echo json_encode(['success' => false, 'message' => 'Aksi api_action tidak dikenal atau kosong.']);
        exit;
}

// 6. EKSEKUSI KE SERVER KAJE MENGGUNAKAN CURL
if ($endpoint) {
    $url = $baseUrl . $endpoint;
    $ch = curl_init($url);

    // Konfigurasi cURL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    
    // Bypass SSL (Sering bermasalah di hosting lokal/shared)
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
    // User Agent untuk menghindari blokir firewall
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');

    // Header Wajib
    $headers = [
        'x-api-key: ' . $apiKey,
        'accept: application/json',
        'Content-Type: application/json'
    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // Payload (Jika kosong, kirim '{}' sesuai syarat API Kaje)
    $payload = !empty($inputData) ? json_encode($inputData) : "{}";
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

    // Jalankan
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $err = curl_error($ch);
    curl_close($ch);

    // 7. OUTPUT KE FRONTEND
    if ($err) {
        echo json_encode(['success' => false, 'message' => 'CURL Error: ' . $err]);
    } else {
        // Teruskan response asli dari server Kaje
        echo $response;
    }
}