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

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

$berita_result = api_get_berita($token);
echo "<h2>Simple Photo Debug</h2>";

if ($berita_result['success']) {
    $data = $berita_result['data'];
    $items = $data['data'] ?? $data;

    if (is_array($items) && count($items) > 0) {
        echo "<h3>First Item Structure:</h3>";
        echo "<pre>";
        print_r($items[0]);
        echo "</pre>";

        echo "<h3>Photo Field Analysis:</h3>";
        $first_item = $items[0];
        foreach ($first_item as $key => $value) {
            if (is_string($value)) {
                echo "<p><strong>$key:</strong> '$value'";
                if (preg_match('/\.(jpg|jpeg|png|gif|webp)$/i', $value)) {
                    echo " <span style='color: green; font-weight: bold;'>(IMAGE FILE)</span>";
                }
                echo "</p>";
            } else {
                echo "<p><strong>$key:</strong> " . gettype($value) . "</p>";
            }
        }

        // Test photo URL
        $photo = $first_item['photo'] ?? '';
        if ($photo && ($photo_url = uploads_berita_url((string) $photo)) !== '') {
            echo "<h3>Photo URL Test:</h3>";
            echo "<p>Full URL: <a href='" . htmlspecialchars($photo_url) . "' target='_blank'>" . htmlspecialchars($photo_url) . "</a></p>";
            echo "<p>Image preview:</p>";
            echo "<img src='" . htmlspecialchars($photo_url) . "' style='max-width: 200px; max-height: 200px; border: 1px solid #ccc;' onerror=\"this.style.display='none'; this.nextElementSibling.style.display='block';\" />";
            echo "<p style='display: none; color: red;'>Gambar tidak dapat dimuat</p>";
        } else {
            echo "<p style='color: red;'>Tidak ada field 'photo' ditemukan</p>";
        }
    }
} else {
    echo "<p>Error: " . ($berita_result['data']['pesan'] ?? 'Unknown error') . "</p>";
}
