<?php
// KONFIGURASI API KAJE STORE
$apiKey = '8eb9026f46a9ebed7c3de2292bd6353fea402c2ae8328f04b728f879a963';
$baseUrl = 'https://end.kaje-store.com';
$endpoint = '/api/service/list-package-otp';

// PROSES CURL
$ch = curl_init($baseUrl . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([])); // Parameter kosong untuk list paket
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'x-api-key: ' . $apiKey,
    'accept: application/json',
    'Content-Type: application/json'
]);

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

// DECODE JSON UNTUK DITAMPILKAN ULANG DENGAN RAPI
$decoded = json_decode($response, true);
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Debug JSON - Akrab Kaje Store</title>
    <style>
        body { 
            font-family: 'Courier New', Courier, monospace; 
            background-color: #1e1e1e; 
            color: #d4d4d4; 
            padding: 20px; 
            line-height: 1.5;
        }
        h2 { color: #569cd6; margin-top: 0; }
        .status-box { 
            background: #2d2d2d; 
            padding: 15px; 
            border-radius: 8px; 
            margin-bottom: 20px;
            display: inline-block;
            border-left: 5px solid <?php echo ($httpCode == 200) ? '#4caf50' : '#f44336'; ?>;
        }
        .status-box span { color: #9cdcfe; }
        pre { 
            background-color: #252526; 
            padding: 20px; 
            border-radius: 8px; 
            overflow-x: auto;
            border: 1px solid #333;
            color: #ce9178; /* Warna teks JSON */
        }
        .key { color: #9cdcfe; font-weight: bold; }
        .string { color: #ce9178; }
        .number { color: #b5cea8; }
        .boolean { color: #569cd6; }
        .null { color: #569cd6; font-style: italic; }
    </style>
</head>
<body>

    <h2>🛠️ Debug Tool: Raw JSON Daftar Paket OTP</h2>
    
    <div class="status-box">
        <div><span>Endpoint:</span> <?php echo $baseUrl . $endpoint; ?></div>
        <div><span>HTTP Status:</span> <strong style="color:<?php echo ($httpCode == 200) ? '#4caf50' : '#f44336'; ?>;"><?php echo $httpCode; ?></strong></div>
    </div>

    <h3>⬇️ Respons Mentah (JSON Pretty Print):</h3>
    
    <pre><?php 
        if ($decoded) {
            // Encode ulang dengan format Pretty Print agar mudah dibaca
            $prettyJson = json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
            
            // Regex sederhana untuk mewarnai Syntax JSON agar mirip Code Editor
            $prettyJson = preg_replace('/"([^"]+)":/', '<span class="key">"$1"</span>:', $prettyJson);
            $prettyJson = preg_replace('/:\s*"([^"]*)"/', ': <span class="string">"$1"</span>', $prettyJson);
            $prettyJson = preg_replace('/:\s*([0-9]+(?:\.[0-9]+)?)/', ': <span class="number">$1</span>', $prettyJson);
            $prettyJson = preg_replace('/:\s*(true|false)/', ': <span class="boolean">$1</span>', $prettyJson);
            $prettyJson = preg_replace('/:\s*null/', ': <span class="null">null</span>', $prettyJson);
            
            echo $prettyJson;
        } else {
            echo htmlspecialchars($response);
        }
    ?></pre>

</body>
</html>