<?php
// Test berita tanpa extension interference
header('Content-Type: text/plain');

require_once __DIR__ . '/../config.php';

echo "=== BERITA TEST CLEAN ===\n\n";

// Login fresh
$login_result = api_login('Widia', 'qwerty5*');
if ($login_result['success']) {
    $token = $login_result['data']['token'];
    echo "✅ Login berhasil\n";
    echo "Token: " . substr($token, 0, 20) . "...\n\n";

    // Call API directly
    $ch = curl_init(API_BASE . 'berita');
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => ['token' => $token],
        CURLOPT_TIMEOUT => 15,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
    ]);

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

    echo "HTTP Code: $httpCode\n";
    echo "Raw Response Length: " . strlen($response) . " bytes\n\n";

    $data = json_decode($response, true);
    echo "JSON Decode Success: " . (json_last_error() === JSON_ERROR_NONE ? 'YES' : 'NO') . "\n";
    echo "JSON Error: " . json_last_error_msg() . "\n\n";

    if ($data && isset($data['data'])) {
        echo "Data Count: " . count($data['data']) . "\n";
        if (!empty($data['data'])) {
            $first = $data['data'][0];
            echo "\n=== FIRST ITEM ===\n";
            echo "Judul: " . ($first['judul'] ?? 'N/A') . "\n";
            echo "Tanggal: " . ($first['tanggal'] ?? 'N/A') . "\n";
            echo "Isi: " . substr(strip_tags($first['isi'] ?? 'N/A'), 0, 100) . "...\n";

            echo "\n=== ALL FIELDS ===\n";
            foreach ($first as $key => $value) {
                echo "$key: " . (is_string($value) ? substr($value, 0, 50) . "..." : $value) . "\n";
            }
        }
    } else {
        echo "❌ No data found\n";
    }
} else {
    echo "❌ Login gagal: " . json_encode($login_result['data']) . "\n";
}
