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

// Check if user is logged in
if (!isset($_SESSION['token'])) {
    header('Location: login.php');
    exit;
}

$token = $_SESSION['token'];

echo "<h2>Debug Berita Photo Structure</h2>";

// Get berita data
$berita_result = api_get_berita($token);

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

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

    if (is_array($berita_items)) {
        echo "<h3>Photo Fields Analysis:</h3>";
        foreach ($berita_items as $index => $item) {
            echo "<h4>Item $index:</h4>";
            echo "<pre>";
            print_r($item);
            echo "</pre>";

            // Check all possible photo fields
            $photo_fields = ['photo', 'foto', 'image', 'gambar', 'img', 'thumbnail', 'preview'];
            echo "<h5>Photo Fields Check:</h5>";
            foreach ($photo_fields as $field) {
                $value = $item[$field] ?? 'NOT_FOUND';
                echo "<p><strong>$field:</strong> " . htmlspecialchars($value) . "</p>";
            }

            // Check if there are any fields that might contain image data
            echo "<h5>All Fields:</h5>";
            foreach ($item as $key => $value) {
                if (is_string($value) && (strpos($value, '.jpg') !== false || strpos($value, '.jpeg') !== false || strpos($value, '.png') !== false || strpos($value, '.gif') !== false)) {
                    echo "<p style='color: green;'><strong>$key:</strong> " . htmlspecialchars($value) . " (POSSIBLE IMAGE)</p>";
                } else {
                    echo "<p><strong>$key:</strong> " . htmlspecialchars(is_string($value) ? $value : gettype($value)) . "</p>";
                }
            }
            echo "<hr>";
        }
    }
} else {
    echo "<p style='color: red;'>API call failed: " . ($berita_result['data']['pesan'] ?? 'Unknown error') . "</p>";
}

echo "<p><a href='dashboard.php'>← Kembali ke Dashboard</a></p>";
