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

$token = $_SESSION['token'] ?? null;
if (!$token) {
    echo "No token found";
    exit;
}

echo "<h2>Check Berita JSON Response</h2>";

$berita_result = api_get_berita($token);

echo "<h3>Full API Response:</h3>";
echo "<pre>";
print_r($berita_result);
echo "</pre>";

if ($berita_result['success']) {
    $data = $berita_result['data'];
    echo "<h3>Data Structure:</h3>";
    echo "<pre>";
    print_r($data);
    echo "</pre>";

    $items = $data['data'] ?? $data;
    if (is_array($items) && count($items) > 0) {
        echo "<h3>First Item Keys:</h3>";
        $first_item = $items[0];
        echo "<ul>";
        foreach (array_keys($first_item) as $key) {
            echo "<li><strong>$key</strong>: " . gettype($first_item[$key]) . "</li>";
        }
        echo "</ul>";

        echo "<h3>First Item Values:</h3>";
        echo "<pre>";
        print_r($first_item);
        echo "</pre>";

        // Check specifically for photo-related fields
        echo "<h3>Photo Field Check:</h3>";
        $photo_fields = ['photo', 'foto', 'image', 'gambar', 'img', 'thumbnail', 'preview', 'cover'];
        foreach ($photo_fields as $field) {
            if (isset($first_item[$field])) {
                $value = $first_item[$field];
                echo "<p><strong>$field:</strong> " . htmlspecialchars($value) . " (" . gettype($value) . ")</p>";

                if (is_string($value) && !empty($value) && ($u = uploads_berita_url($value)) !== '') {
                    echo "<p>Test URL: <a href='" . htmlspecialchars($u) . "' target='_blank'>" . htmlspecialchars($u) . "</a></p>";
                }
            } else {
                echo "<p><strong>$field:</strong> NOT FOUND</p>";
            }
        }
    }
} else {
    echo "<p style='color: red;'>API Error: " . ($berita_result['data']['pesan'] ?? 'Unknown error') . "</p>";
}
