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

echo "<h2>Test API Berita Function</h2>";

// Login dulu
$login_result = api_login('Widia', 'qwerty5*');
if ($login_result['success']) {
    $token = $login_result['data']['token'];

    echo "<h3>Login Success, Token: " . substr($token, 0, 20) . "...</h3>";

    // 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>";
    echo "Raw Response: <pre>" . $berita_result['raw_response'] . "</pre>";

    echo "<h3>Decoded Data:</h3>";
    echo "<pre>" . print_r($berita_result['data'], true) . "</pre>";

    if ($berita_result['success'] && isset($berita_result['data'])) {
        $berita_data = $berita_result['data'];
        echo "<h3>Data Access Test:</h3>";
        echo "Is Array: " . (is_array($berita_data) ? 'YES' : 'NO') . "<br>";
        echo "Count: " . (is_array($berita_data) ? count($berita_data) : 'N/A') . "<br>";

        if (is_array($berita_data) && !empty($berita_data)) {
            $first_berita = $berita_data[0];
            echo "<h4>First Berita Item:</h4>";
            echo "Raw: <pre>" . print_r($first_berita, true) . "</pre>";
            echo "Judul: " . ($first_berita['judul'] ?? 'N/A') . "<br>";
            echo "Tanggal: " . ($first_berita['tanggal'] ?? 'N/A') . "<br>";
            echo "Isi: " . substr(strip_tags($first_berita['isi'] ?? 'N/A'), 0, 100) . "...<br>";
        }
    }
} else {
    echo "Login gagal: " . json_encode($login_result['data']);
}
