<?php
/**
 * Block Registry - Category Blocks Admin Page
 * Manages blocks for a specific category
 */

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

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

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

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

$category = (new \Mtc\Shop\Category())->find($id);
if (empty($category->id)) {
    header('Location: /shop/admin/categories/manage.categories.php');
    exit();
}

if (empty($category->is_online_doctor)) {
    header('Location: /shop/admin/categories/edit.category.php?id=' . $category->id);
    exit();
}

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

// Render the entity blocks section
$entity_type = 'category';
$entity_id = $category->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']);
