<?php
session_start();
require_once __DIR__ . '/../config.php';

echo "<h2>Debug API Request & Response</h2>";

// Login dulu untuk dapat token
echo "<h3>1. Login Process:</h3>";
$login_result = api_login('Widia', 'qwerty5*');
echo "<pre>";
echo "Login Success: " . ($login_result['success'] ? 'YES' : 'NO') . "\n";
echo "HTTP Code: " . $login_result['http_code'] . "\n";
echo "Raw Response: " . $login_result['raw_response'] . "\n";
echo "</pre>";

if (!$login_result['success']) {
    echo "<p style='color:red'>Login gagal, tidak bisa lanjut test berita</p>";
    exit;
}

$token = $login_result['data']['token'];
echo "<p style='color:green'>✅ Token: " . substr($token, 0, 20) . "...</p>";

echo "<hr>";

// Test berita dengan debug detail
echo "<h3>2. Berita API Request:</h3>";

// Simulasi request yang akan dikirim
$endpoint = 'berita';
$body = ['token' => $token];
$url = API_BASE . $endpoint;

echo "<pre>";
echo "URL: $url\n";
echo "Method: POST\n";
echo "Body: " . json_encode($body) . "\n";
echo "Content-Type: application/x-www-form-urlencoded\n";
echo "</pre>";

echo "<h3>3. Berita API Response:</h3>";

// Panggil API berita
$berita_result = api_get_berita($token);

echo "<pre>";
echo "Success: " . ($berita_result['success'] ? 'YES' : 'NO') . "\n";
echo "HTTP Code: " . $berita_result['http_code'] . "\n";
echo "Raw Response: " . $berita_result['raw_response'] . "\n";
echo "</pre>";

echo "<h3>4. Parsed Data:</h3>";
echo "<pre>";
echo "Data Type: " . gettype($berita_result['data']) . "\n";
echo "Data Count: " . (is_array($berita_result['data']) ? count($berita_result['data']) : 'N/A') . "\n";
echo "Data Structure:\n";
print_r($berita_result['data']);
echo "</pre>";

echo "<h3>5. Berita Items:</h3>";

if ($berita_result['success'] && !empty($berita_result['data'])) {
    $data = $berita_result['data'];

    // Cek struktur data
    if (isset($data['data']) && is_array($data['data'])) {
        $berita_items = $data['data'];
        echo "<p style='color:green'>✅ Data ditemukan di response['data']['data']</p>";
    } elseif (is_array($data) && isset($data[0])) {
        $berita_items = $data;
        echo "<p style='color:green'>✅ Data ditemukan di response['data'] langsung</p>";
    } else {
        $berita_items = [];
        echo "<p style='color:red'>❌ Struktur data tidak dikenali</p>";
    }

    if (!empty($berita_items)) {
        echo "<p>Jumlah berita: " . count($berita_items) . "</p>";

        foreach (array_slice($berita_items, 0, 3) as $index => $item) {
            echo "<div style='border:1px solid #ccc;padding:10px;margin:10px 0;background:#f9f9f9;'>";
            echo "<h4>Berita " . ($index + 1) . ":</h4>";
            echo "<pre>";
            print_r($item);
            echo "</pre>";

            // Tampilkan format yang diinginkan
            $judul = htmlspecialchars($item['judul'] ?? '-');
            $tanggal = htmlspecialchars($item['tanggal'] ?? '-');
            $isi = htmlspecialchars(trim($item['isi'] ?? ''));
            $photoUrl = uploads_berita_url((string) ($item['photo'] ?? ''));

            echo "<div style='border:1px solid #ddd;padding:10px;margin:5px 0;background:white;'>";
            echo "<h4 style='margin:0 0 5px 0;'>📅 $tanggal — $judul</h4>";
            if ($photoUrl !== '') {
                echo "<img src='" . htmlspecialchars($photoUrl) . "' alt='$judul' style='max-width:250px;border-radius:8px;display:block;margin:8px 0;'>";
            }
            echo "<p>" . nl2br($isi) . "</p>";
            echo "</div>";
            echo "</div>";
        }
    } else {
        echo "<p style='color:red'>❌ Tidak ada item berita ditemukan</p>";
    }
} else {
    echo "<p style='color:red'>❌ API berita gagal atau tidak mengembalikan data</p>";
}

echo "<hr>";
echo "<h3>6. Debug Info:</h3>";
echo "<pre>";
echo "Session Token: " . ($_SESSION['token'] ?? 'Tidak ada') . "\n";
echo "Current Time: " . date('Y-m-d H:i:s') . "\n";
echo "PHP Version: " . phpversion() . "\n";
echo "</pre>";
