<?php
/**
 * Block Registry - Product Blocks Admin Page
 * Manages blocks for a specific product (item)
 */

$path = '../../../../';
require_once $path . 'core/includes/header.inc.php';

$page_meta['title'] = 'Product Blocks';
$messages = [];

$id = (int) filter_input(INPUT_GET, 'id');

if (empty($id)) {
    header('Location: /shop/admin/products/manage.products.php');
    exit();
}

$item = new \Item($id);
if (empty($item->id)) {
    header('Location: /shop/admin/products/manage.products.php');
    exit();
}

// Capture the product tabnav HTML
ob_start();
require_once $path . 'shop/admin/products/tabnav.php';
$tabnav = ob_get_clean();

// Render the entity blocks section
$entity_type = 'item';
$entity_id = $item->id;

ob_start();
require __DIR__ . '/../templates/entity_blocks.php';
$entity_blocks_html = ob_get_clean();

// Session messages
if (!empty($_SESSION['message'])) {
    $messages[] = $_SESSION['message'];
}

/** @var \Twig\Environment $twig */
$twig->display('BlockRegistry/admin/entity_blocks_page.twig', [
    'page_meta' => $page_meta,
    'tabnav' => $tabnav,
    'entity_type' => $entity_type,
    'entity_id' => $entity_id,
    'entity_blocks_html' => $entity_blocks_html,
    'messages' => $messages,
]);

unset($_SESSION['message']);
