<?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'];
$berita_id = $_GET['id'] ?? '';

echo "<h2>Debug Detail Info</h2>";
echo "<p><strong>ID yang dicari:</strong> " . htmlspecialchars($berita_id) . "</p>";

// Get berita data
$berita_result = api_get_berita($token);
echo "<h3>API Response:</h3>";
echo "<pre>";
print_r($berita_result);
echo "</pre>";

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

    // Check structure
    $berita_items = $berita_data['data'] ?? $berita_data;
    echo "<h3>Berita Items (extracted):</h3>";
    echo "<pre>";
    print_r($berita_items);
    echo "</pre>";

    if (is_array($berita_items)) {
        echo "<h3>Available IDs:</h3>";
        foreach ($berita_items as $index => $item) {
            $item_id = $item['id'] ?? 'NO_ID';
            $item_judul = $item['judul'] ?? 'NO_TITLE';
            echo "<p>Index $index: ID = '$item_id', Judul = '$item_judul'</p>";
        }

        // Try to find the specific ID
        echo "<h3>Searching for ID: '$berita_id'</h3>";
        $found = false;
        foreach ($berita_items as $item) {
            if (($item['id'] ?? '') == $berita_id) {
                echo "<p style='color: green; font-weight: bold;'>FOUND! Item details:</p>";
                echo "<pre>";
                print_r($item);
                echo "</pre>";
                $found = true;
                break;
            }
        }

        if (!$found) {
            echo "<p style='color: red; font-weight: bold;'>ID '$berita_id' NOT FOUND in the data!</p>";
        }
    }
} else {
    echo "<p style='color: red;'>API call failed: " . ($berita_result['data']['pesan'] ?? 'Unknown error') . "</p>";
}

echo "<p><a href='detail-info.php?id=$berita_id'>← Kembali ke Detail Info</a></p>";
