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

echo "<h2>Fresh Berita Test</h2>";

// Fresh login
$login_result = api_login('Widia', 'qwerty5*');
if ($login_result['success']) {
    $token = $login_result['data']['token'];
    echo "✅ Fresh login berhasil<br>";
    echo "Token: " . substr($token, 0, 20) . "...<br><br>";

    // Test api_get_berita
    $berita_result = api_get_berita($token);

    echo "<h3>API Get Berita Result:</h3>";
    echo "Success: " . ($berita_result['success'] ? 'YES' : 'NO') . "<br>";
    echo "HTTP Code: " . $berita_result['http_code'] . "<br>";

    if ($berita_result['success']) {
        $berita_data = $berita_result['data'];
        echo "Data Type: " . gettype($berita_data) . "<br>";
        echo "Data Count: " . (is_array($berita_data) ? count($berita_data) : 'N/A') . "<br>";

        if (is_array($berita_data) && !empty($berita_data)) {
            echo "<h3>First Berita Item:</h3>";
            $first = $berita_data[0];
            echo "Raw: <pre>" . print_r($first, true) . "</pre>";
            echo "Judul: " . ($first['judul'] ?? 'N/A') . "<br>";
            echo "Tanggal: " . ($first['tanggal'] ?? 'N/A') . "<br>";
            echo "Isi: " . substr(strip_tags($first['isi'] ?? 'N/A'), 0, 100) . "...<br>";
        } else {
            echo "❌ Data kosong atau bukan array<br>";
        }
    } else {
        echo "❌ API call gagal<br>";
        echo "Error: " . json_encode($berita_result['data']) . "<br>";
    }
} else {
    echo "❌ Login gagal: " . json_encode($login_result['data']) . "<br>";
}
