<?php
// pages/layout.php
// Defines the page layout: main content with optional secondary column (submenu)
// Accepts raw HTML content or a file path to include. Same for $submenu.

if (!defined('BEEZUI_ROOT')) {
    // This file resides in <root>/pages
    define('BEEZUI_ROOT', dirname(__DIR__));
}



function layout($containerId = 'main', $content = 'no content', $submenu = null, $secondaryWidth = '72')
{
    // Resolve content HTML (string or include file)
    $contentHtml = '';
    // to avoid  WARNING: is_file(): File name is longer than the maximum allowed path length on this platform (4096): 
    // make a check if $content looks like a file path
    $isProbablyPath = (is_string($content) && $content !== '' && strlen($content) < 4096 && strpos($content, '<') === false && strpos($content, "\n") === false);
    if ($isProbablyPath && is_file($content)) {

        ob_start();
        include $content;
        $contentHtml = ob_get_clean();
    } else {
        $contentHtml = (string)$content;
    }

    // Resolve submenu HTML (string or include file)
    $submenuHtml = '';
    if (is_string($submenu) && $submenu !== '') {
        $isSubmenuProbablyPath = (strlen($submenu) < 4096 && strpos($submenu, '<') === false && strpos($submenu, "\n") === false);
        $submenuPath = $submenu;
        if ($isSubmenuProbablyPath) {
            // If it's a file path (absolute or relative to project root), try to include
            // Allow paths relative to project root or to this pages directory
            if (!is_file($submenuPath)) {
                $rootRelative = BEEZUI_ROOT . DIRECTORY_SEPARATOR . ltrim($submenu, '/');
                if (is_file($rootRelative)) {
                    $submenuPath = $rootRelative;
                } else {
                    $pagesRelative = BEEZUI_ROOT . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . ltrim($submenu, '/');
                    if (is_file($pagesRelative)) {
                        $submenuPath = $pagesRelative;
                    }
                }
            }
        }
        if ($isSubmenuProbablyPath && is_file($submenuPath)) {
            ob_start();
            include $submenuPath;
            $submenuHtml = ob_get_clean();
        } else {
            // Treat as raw HTML
            $submenuHtml = (string)$submenu;
        }
    }

    $hasSubmenu = trim($submenuHtml) !== '';
    $asideWidthClass = 'w-' . $secondaryWidth;
    $mainInnerPadClass = $hasSubmenu ? ('ml-' . $secondaryWidth) : '';
    $asideVisibilityClass = $hasSubmenu ? 'block' : 'hidden';

    ob_start();
    ?>
<!--        help tailwind to generate the correct classes -->
    <div class="w-96 ml-96 left-96 hidden"></div>
    <div class="w-72 ml-72 left-72 hidden"></div>
    <div class="w-64 ml-64 left-64 hidden"></div>
    <div class="w-60 ml-60 left-60 hidden"></div>
    <container id="app-container-<?php echo $containerId; ?>" class="z-0">
        <main class=" transition-all duration-300 ease-in-out">
            <div id="main-inner" class="<?php echo $mainInnerPadClass ?> relative overflow-scroll">

                <div class="px-2 py-4 sm:px-4 lg:px-6 lg:py-6  min-w-xl">

                    <div id="app-main-<?php echo $containerId; ?>" class="min-h-48">
                        <?php echo $contentHtml ?>
                    </div>
                </div>
            </div>
        </main>
        <?php if ($hasSubmenu) { ?>
            <aside id="secondary-column"
                   class="absolute inset-y-0 <?php echo $asideWidthClass ?> overflow-y-auto  px-4 py-6 sm:px-6 lg:px-8 <?php echo $asideVisibilityClass ?> transition-all duration-300 ease-in-out dark:border-white/10">
                <div id="secondary-column-body">
                    <?php echo $submenuHtml ?>
                </div>
            </aside>
        <?php } ?>

    </container>
    <?php
    return ob_get_clean();
}
