<?php
/**
 * ZERTI admin shell (nav, panels, page render).
 */
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Current in-app nav tab from the query string.
 */
function zerti_get_current_nav_tab(): string
{
    return zerti_route_from_request()['tab'];
}

/**
 * Sidebar badge: count of orders in «processing» status (shop tab only).
 */
function zerti_render_shop_nav_processing_badge(): void
{
    if (!function_exists('zerti_shop_processing_orders_count') || !function_exists('zerti_shop_processing_orders_badge_label')) {
        return;
    }

    $count = zerti_shop_processing_orders_count();
    $hidden = $count < 1;
    $label = zerti_shop_processing_orders_badge_label();
    $aria = $hidden
        ? ''
        : sprintf(
            /* translators: %s: order count (Persian digits, may include +) */
            __('Processing orders: %s', 'zerti'),
            $label
        );
    ?>
    <span
        class="zerti-nav-processing-badge<?php echo $hidden ? ' d-none' : ''; ?>"
        data-zerti-processing-badge="1"
        <?php echo $hidden ? 'aria-hidden="true"' : 'role="status"'; ?>
        <?php echo $aria !== '' ? ' aria-label="' . esc_attr($aria) . '"' : ''; ?>
    ><?php echo esc_html($label); ?></span>
    <?php
}

/**
 * Sidebar badge: count of pending product reviews (WooCommerce comment status: hold).
 */
function zerti_render_customer_relations_pending_reviews_badge(): void
{
    if (!function_exists('zerti_product_pending_reviews_count') || !function_exists('zerti_product_pending_reviews_badge_label')) {
        return;
    }

    $count = zerti_product_pending_reviews_count();
    $hidden = $count < 1;
    $label = zerti_product_pending_reviews_badge_label();
    $aria = $hidden
        ? ''
        : sprintf(
            /* translators: %s: pending review count (Persian digits, may include +) */
            __('Reviews pending review: %s', 'zerti'),
            $label
        );
    ?>
    <span
        class="zerti-nav-processing-badge<?php echo $hidden ? ' d-none' : ''; ?>"
        data-zerti-pending-reviews-badge="1"
        <?php echo $hidden ? 'aria-hidden="true"' : 'role="status"'; ?>
        <?php echo $aria !== '' ? ' aria-label="' . esc_attr($aria) . '"' : ''; ?>
    ><?php echo esc_html($label); ?></span>
    <?php
}

/**
 * Sidebar badge: count of pending product comments.
 */
function zerti_render_customer_relations_pending_comments_badge(): void
{
    if (!function_exists('zerti_product_pending_comments_count') || !function_exists('zerti_product_pending_comments_badge_label')) {
        return;
    }

    $count = zerti_product_pending_comments_count();
    $hidden = $count < 1;
    $label = zerti_product_pending_comments_badge_label();
    $aria = $hidden
        ? ''
        : sprintf(
            /* translators: %s: pending comment count (Persian digits, may include +) */
            __('Comments pending review: %s', 'zerti'),
            $label
        );
    ?>
    <span
        class="zerti-nav-processing-badge<?php echo $hidden ? ' d-none' : ''; ?>"
        data-zerti-pending-comments-badge="1"
        <?php echo $hidden ? 'aria-hidden="true"' : 'role="status"'; ?>
        <?php echo $aria !== '' ? ' aria-label="' . esc_attr($aria) . '"' : ''; ?>
    ><?php echo esc_html($label); ?></span>
    <?php
}

/**
 * Sidebar badge: combined pending reviews + comments (Customer affairs parent).
 */
function zerti_render_customer_relations_pending_total_badge(): void
{
    if (
        !function_exists('zerti_product_pending_customer_relations_count')
        || !function_exists('zerti_product_pending_customer_relations_badge_label')
    ) {
        return;
    }

    $count = zerti_product_pending_customer_relations_count();
    $hidden = $count < 1;
    $label = zerti_product_pending_customer_relations_badge_label();
    $aria = $hidden
        ? ''
        : sprintf(
            /* translators: %s: combined pending reviews and comments count (Persian digits, may include +) */
            __('Pending reviews and comments: %s', 'zerti'),
            $label
        );
    ?>
    <span
        class="zerti-nav-processing-badge<?php echo $hidden ? ' d-none' : ''; ?>"
        data-zerti-pending-customer-relations-badge="1"
        <?php echo $hidden ? 'aria-hidden="true"' : 'role="status"'; ?>
        <?php echo $aria !== '' ? ' aria-label="' . esc_attr($aria) . '"' : ''; ?>
    ><?php echo esc_html($label); ?></span>
    <?php
}

/**
 * Primary sidebar / offcanvas navigation — buttons switch panels without full page reload.
 *
 * @param string $current_tab Active top-level tab slug.
 * @param string $nav_instance_suffix Unique suffix for disclosure ids (two menus: offcanvas + desktop).
 */
function zerti_render_admin_nav_pills(string $current_tab, string $nav_instance_suffix = 'main'): void
{
    $nav_instance_suffix = preg_replace('/[^a-z0-9_-]/i', '', $nav_instance_suffix);
    if ($nav_instance_suffix === '') {
        $nav_instance_suffix = 'main';
    }
    $fin_active = $current_tab === 'financial' ? zerti_get_financial_sub() : '';
    $reports_nav_active = function_exists('zerti_is_reports_nav_active')
        && zerti_is_reports_nav_active($current_tab, $fin_active);
    ?>
                <nav class="nav nav-pills flex-column gap-1 px-3 pb-3 zerti-admin-nav-pills" role="tablist" aria-label="<?php esc_attr_e('ZERTI sections', 'zerti'); ?>">
                    <?php foreach (zerti_admin_nav_tabs() as $item) : ?>
                        <?php
                        $slug = $item['slug'];
                        if ($slug === 'financial') {
                            $active = $current_tab === 'financial' && !$reports_nav_active;
                        } elseif ($slug === 'reports') {
                            $active = $reports_nav_active;
                        } else {
                            $active = $slug === $current_tab;
                        }
                        ?>
                        <?php if ($slug === 'inventory') : ?>
                            <?php
                            $inv_active = zerti_get_inventory_sub();
                            $inv_wrap_id = 'zerti-inv-submenu-' . $nav_instance_suffix;
                            $inv_expanded = $active;
                            $inv_subs = zerti_routes()['inventory']['subs'];
                            $inv_tab_href = zerti_admin_route_url(['tab' => 'inventory', 'inv' => $inv_active]);
                            ?>
                            <div class="zerti-nav-group-inventory zerti-nav-submenu-group d-flex flex-column gap-1 w-100">
                                <a href="<?php echo esc_url($inv_tab_href); ?>"
                                   class="nav-link zerti-nav-tab text-start w-100 border-0<?php echo $active ? ' active' : ''; ?>"
                                   data-zerti-tab="<?php echo esc_attr($slug); ?>"
                                   role="tab"
                                   aria-selected="<?php echo $active ? 'true' : 'false'; ?>"
                                   aria-controls="<?php echo esc_attr('zerti-panel-' . $slug); ?>"
                                   aria-expanded="<?php echo $inv_expanded ? 'true' : 'false'; ?>"
                                   id="<?php echo esc_attr('zerti-nav-inv-trigger-' . $nav_instance_suffix); ?>">
                                    <i class="fa-solid <?php echo esc_attr($item['icon']); ?> ms-2" aria-hidden="true"></i>
                                    <span><?php echo esc_html($item['label']); ?></span>
                                </a>
                                <div id="<?php echo esc_attr($inv_wrap_id); ?>"
                                     class="zerti-inv-submenu-wrap zerti-nav-submenu-wrap w-100"
                                     role="group"
                                     aria-label="<?php esc_attr_e('Inventory subsections', 'zerti'); ?>"
                                     <?php echo $inv_expanded ? '' : ' hidden'; ?>>
                                    <div class="zerti-inv-submenu zerti-nav-submenu-inner d-flex flex-column gap-1 pe-1 pb-1" role="presentation">
                                        <?php
                                        $inv_product_route = function_exists('zerti_inventory_product_id_from_request')
                                            ? zerti_inventory_product_id_from_request()
                                            : null;
                                        ?>
                                        <?php foreach ($inv_subs as $sub_item) : ?>
                                            <?php
                                            $sub_slug = (string) ($sub_item['slug'] ?? '');
                                            $is_nav_only = !empty($sub_item['nav_only']);
                                            if ($is_nav_only) {
                                                $nav_product = isset($sub_item['product']) ? (int) $sub_item['product'] : 0;
                                                $sub_href = zerti_admin_route_url([
                                                    'tab' => 'inventory',
                                                    'inv' => 'warehouse',
                                                    'product' => $nav_product,
                                                ]);
                                                $sub_active = $active
                                                    && $inv_active === 'warehouse'
                                                    && $inv_product_route === $nav_product;
                                            } else {
                                                $sub_href = zerti_admin_route_url(['tab' => 'inventory', 'inv' => $sub_slug]);
                                                if ($sub_slug === 'warehouse') {
                                                    $sub_active = $active
                                                        && $inv_active === 'warehouse'
                                                        && $inv_product_route === null;
                                                } else {
                                                    $sub_active = $active && $inv_active === $sub_slug;
                                                }
                                            }
                                            ?>
                                            <?php if ($is_nav_only) : ?>
                                                <a href="<?php echo esc_url($sub_href); ?>"
                                                   class="nav-link zerti-inventory-nav-action zerti-product-add-btn zerti-nav-submenu-item text-start w-100 border-0 py-1 px-2 small rounded<?php echo $sub_active ? ' active' : ''; ?>"
                                                   data-zerti-inv-nav="<?php echo esc_attr($sub_slug); ?>"
                                                   data-zerti-product="<?php echo esc_attr((string) $nav_product); ?>"
                                                   role="tab"
                                                   aria-selected="<?php echo $sub_active ? 'true' : 'false'; ?>">
                                                    <i class="fa-solid <?php echo esc_attr((string) ($sub_item['icon'] ?? 'fa-plus')); ?> ms-2" aria-hidden="true"></i>
                                                    <span><?php echo esc_html((string) ($sub_item['label'] ?? '')); ?></span>
                                                </a>
                                            <?php else : ?>
                                                <a href="<?php echo esc_url($sub_href); ?>"
                                                   class="nav-link zerti-inventory-subtab zerti-nav-submenu-item text-start w-100 border-0 py-1 px-2 small rounded<?php echo $sub_active ? ' active' : ''; ?>"
                                                   data-zerti-inv-sub="<?php echo esc_attr($sub_slug); ?>"
                                                   role="tab"
                                                   aria-selected="<?php echo $sub_active ? 'true' : 'false'; ?>">
                                                    <i class="fa-solid <?php echo esc_attr((string) ($sub_item['icon'] ?? '')); ?> ms-2" aria-hidden="true"></i>
                                                    <span><?php echo esc_html((string) ($sub_item['label'] ?? '')); ?></span>
                                                </a>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </div>
                        <?php elseif ($slug === 'customer_relations') : ?>
                            <?php
                            $crm_active = function_exists('zerti_is_customer_relations_panel_tab')
                                && zerti_is_customer_relations_panel_tab($current_tab);
                            $crm_wrap_id = 'zerti-crm-submenu-' . $nav_instance_suffix;
                            $crm_expanded = $crm_active;
                            $crm_subs = zerti_routes()['customer_relations']['subs'];
                            ?>
                            <div class="zerti-nav-group-crm zerti-nav-submenu-group d-flex flex-column gap-1 w-100">
                                <button type="button"
                                        class="nav-link zerti-nav-tab zerti-nav-tab--disclosure text-start w-100 border-0<?php echo $crm_active ? ' active' : ''; ?>"
                                        data-zerti-tab="<?php echo esc_attr($slug); ?>"
                                        role="tab"
                                        aria-selected="<?php echo $crm_active ? 'true' : 'false'; ?>"
                                        aria-expanded="<?php echo $crm_expanded ? 'true' : 'false'; ?>"
                                        id="<?php echo esc_attr('zerti-nav-crm-trigger-' . $nav_instance_suffix); ?>">
                                    <i class="fa-solid <?php echo esc_attr($item['icon']); ?> ms-2" aria-hidden="true"></i>
                                    <span><?php echo esc_html($item['label']); ?></span>
                                    <?php zerti_render_customer_relations_pending_total_badge(); ?>
                                </button>
                                <div id="<?php echo esc_attr($crm_wrap_id); ?>"
                                     class="zerti-crm-submenu-wrap zerti-nav-submenu-wrap w-100"
                                     role="group"
                                     aria-label="<?php esc_attr_e('Customer affairs subsections', 'zerti'); ?>"
                                     <?php echo $crm_expanded ? '' : ' hidden'; ?>>
                                    <div class="zerti-crm-submenu zerti-nav-submenu-inner d-flex flex-column gap-1 pe-1 pb-1" role="presentation">
                                        <?php foreach ($crm_subs as $sub_item) : ?>
                                            <?php
                                            $panel_tab = (string) ($sub_item['tab'] ?? $sub_item['slug'] ?? '');
                                            if ($panel_tab === '' || !zerti_user_can_access_tab($panel_tab)) {
                                                continue;
                                            }
                                            $sub_active = $current_tab === $panel_tab;
                                            $sub_href = zerti_admin_route_url(['tab' => $panel_tab]);
                                            ?>
                                            <a href="<?php echo esc_url($sub_href); ?>"
                                               class="nav-link zerti-crm-subtab zerti-nav-submenu-item text-start w-100 border-0 py-1 px-2 small rounded<?php echo $sub_active ? ' active' : ''; ?>"
                                               data-zerti-tab="<?php echo esc_attr($panel_tab); ?>"
                                               role="tab"
                                               aria-selected="<?php echo $sub_active ? 'true' : 'false'; ?>"
                                               aria-controls="<?php echo esc_attr('zerti-panel-' . $panel_tab); ?>">
                                                <i class="fa-solid <?php echo esc_attr((string) ($sub_item['icon'] ?? '')); ?> ms-2" aria-hidden="true"></i>
                                                <span><?php echo esc_html((string) ($sub_item['label'] ?? '')); ?></span>
                                                <?php if ($panel_tab === 'reviews') : ?>
                                                    <?php zerti_render_customer_relations_pending_reviews_badge(); ?>
                                                <?php elseif ($panel_tab === 'comments') : ?>
                                                    <?php zerti_render_customer_relations_pending_comments_badge(); ?>
                                                <?php endif; ?>
                                            </a>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </div>
                        <?php elseif ($slug === 'reports') : ?>
                            <?php
                            $reports_wrap_id = 'zerti-reports-submenu-' . $nav_instance_suffix;
                            $reports_expanded = $active;
                            $reports_subs = zerti_routes()['reports']['subs'];
                            ?>
                            <div class="zerti-nav-group-reports zerti-nav-submenu-group d-flex flex-column gap-1 w-100">
                                <button type="button"
                                        class="nav-link zerti-nav-tab zerti-nav-tab--disclosure text-start w-100 border-0<?php echo $active ? ' active' : ''; ?>"
                                        data-zerti-tab="<?php echo esc_attr($slug); ?>"
                                        role="tab"
                                        aria-selected="<?php echo $active ? 'true' : 'false'; ?>"
                                        aria-expanded="<?php echo $reports_expanded ? 'true' : 'false'; ?>"
                                        id="<?php echo esc_attr('zerti-nav-reports-trigger-' . $nav_instance_suffix); ?>">
                                    <i class="fa-solid <?php echo esc_attr($item['icon']); ?> ms-2" aria-hidden="true"></i>
                                    <span><?php echo esc_html($item['label']); ?></span>
                                </button>
                                <div id="<?php echo esc_attr($reports_wrap_id); ?>"
                                     class="zerti-reports-submenu-wrap zerti-nav-submenu-wrap w-100"
                                     role="group"
                                     aria-label="<?php esc_attr_e('Reports subsections', 'zerti'); ?>"
                                     <?php echo $reports_expanded ? '' : ' hidden'; ?>>
                                    <div class="zerti-reports-submenu zerti-nav-submenu-inner d-flex flex-column gap-1 pe-1 pb-1" role="presentation">
                                        <?php foreach ($reports_subs as $sub_item) : ?>
                                            <?php
                                            $sub_slug = (string) ($sub_item['slug'] ?? '');
                                            $sub_active = $reports_nav_active && $fin_active === $sub_slug;
                                            $sub_href = zerti_admin_route_url(['tab' => 'financial', 'fin' => $sub_slug]);
                                            ?>
                                            <a href="<?php echo esc_url($sub_href); ?>"
                                               class="nav-link zerti-reports-subtab zerti-nav-submenu-item text-start w-100 border-0 py-1 px-2 small rounded<?php echo $sub_active ? ' active' : ''; ?>"
                                               data-zerti-fin-sub="<?php echo esc_attr($sub_slug); ?>"
                                               role="tab"
                                               aria-selected="<?php echo $sub_active ? 'true' : 'false'; ?>"
                                               aria-controls="<?php echo esc_attr('zerti-panel-financial'); ?>">
                                                <i class="fa-solid <?php echo esc_attr((string) ($sub_item['icon'] ?? '')); ?> ms-2" aria-hidden="true"></i>
                                                <span class="zerti-nav-tab__label"><?php echo esc_html((string) ($sub_item['label'] ?? '')); ?></span>
                                                <?php do_action('zerti_admin_nav_pro_badge', $sub_item); ?>
                                            </a>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </div>
                        <?php elseif ($slug === 'financial') : ?>
                            <?php
                            $fin_wrap_id = 'zerti-fin-submenu-' . $nav_instance_suffix;
                            $fin_expanded = $active;
                            $fin_subs = zerti_routes()['financial']['subs'];
                            $fin_tab_href = zerti_admin_route_url([
                                'tab' => 'financial',
                                'fin' => $reports_nav_active ? zerti_routes()['financial']['default'] : $fin_active,
                            ]);
                            ?>
                            <div class="zerti-nav-group-financial zerti-nav-submenu-group d-flex flex-column gap-1 w-100">
                                <a href="<?php echo esc_url($fin_tab_href); ?>"
                                   class="nav-link zerti-nav-tab text-start w-100 border-0<?php echo $active ? ' active' : ''; ?>"
                                   data-zerti-tab="<?php echo esc_attr($slug); ?>"
                                   role="tab"
                                   aria-selected="<?php echo $active ? 'true' : 'false'; ?>"
                                   aria-controls="<?php echo esc_attr('zerti-panel-' . $slug); ?>"
                                   aria-expanded="<?php echo $fin_expanded ? 'true' : 'false'; ?>"
                                   id="<?php echo esc_attr('zerti-nav-fin-trigger-' . $nav_instance_suffix); ?>">
                                    <i class="fa-solid <?php echo esc_attr($item['icon']); ?> ms-2" aria-hidden="true"></i>
                                    <span><?php echo esc_html($item['label']); ?></span>
                                </a>
                                <div id="<?php echo esc_attr($fin_wrap_id); ?>"
                                     class="zerti-fin-submenu-wrap zerti-nav-submenu-wrap w-100"
                                     role="group"
                                     aria-label="<?php esc_attr_e('Finance subsections', 'zerti'); ?>"
                                     <?php echo $fin_expanded ? '' : ' hidden'; ?>>
                                    <div class="zerti-fin-submenu zerti-nav-submenu-inner d-flex flex-column gap-1 pe-1 pb-1" role="presentation">
                                        <?php foreach ($fin_subs as $sub_item) : ?>
                                            <?php
                                            $sub_slug = (string) ($sub_item['slug'] ?? '');
                                            $sub_active = $active && $fin_active === $sub_slug;
                                            $sub_href = zerti_admin_route_url(['tab' => 'financial', 'fin' => $sub_slug]);
                                            ?>
                                            <a href="<?php echo esc_url($sub_href); ?>"
                                               class="nav-link zerti-financial-subtab zerti-nav-submenu-item text-start w-100 border-0 py-1 px-2 small rounded<?php echo $sub_active ? ' active' : ''; ?>"
                                               data-zerti-fin-sub="<?php echo esc_attr($sub_slug); ?>"
                                               role="tab"
                                               aria-selected="<?php echo $sub_active ? 'true' : 'false'; ?>">
                                                <i class="fa-solid <?php echo esc_attr((string) ($sub_item['icon'] ?? '')); ?> ms-2" aria-hidden="true"></i>
                                                <span class="zerti-nav-tab__label"><?php echo esc_html((string) ($sub_item['label'] ?? '')); ?></span>
                                                <?php do_action('zerti_admin_nav_pro_badge', $sub_item); ?>
                                            </a>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                            </div>
                        <?php else : ?>
                            <a href="<?php echo esc_url(zerti_admin_route_url(['tab' => $slug])); ?>"
                               class="nav-link zerti-nav-tab text-start w-100 border-0<?php echo $active ? ' active' : ''; ?>"
                               data-zerti-tab="<?php echo esc_attr($slug); ?>"
                               role="tab"
                               aria-selected="<?php echo $active ? 'true' : 'false'; ?>"
                               aria-controls="<?php echo esc_attr('zerti-panel-' . $slug); ?>">
                                <i class="fa-solid <?php echo esc_attr($item['icon']); ?> ms-2" aria-hidden="true"></i>
                                <span class="zerti-nav-tab__label"><?php echo esc_html($item['label']); ?></span>
                                <?php if ($slug === 'shop') : ?>
                                    <?php zerti_render_shop_nav_processing_badge(); ?>
                                <?php endif; ?>
                                <?php if (!empty($item['pro_only'])) : ?>
                                    <?php do_action('zerti_admin_nav_pro_badge', $item); ?>
                                <?php endif; ?>
                            </a>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </nav>
    <?php
}

/**
 * Main content: one panel per tab; active tab SSR, others load via AJAX on first open.
 */
function zerti_render_admin_panels(string $current_tab): void
{
    $tabs = zerti_admin_panel_tabs();
    $last = count($tabs) - 1;
    $route = zerti_route_from_request();
    foreach ($tabs as $i => $item) {
        $slug = $item['slug'];
        $hidden = $slug !== $current_tab;
        $panel_title = $slug === $route['tab']
            ? zerti_panel_nav_title($route)
            : ((string) ($item['label'] ?? ''));
        ?>
                    <section class="zerti-panel<?php echo $i < $last ? ' mb-3' : ''; ?>"
                             id="<?php echo esc_attr('zerti-panel-' . $slug); ?>"
                             data-zerti-panel="<?php echo esc_attr($slug); ?>"
                             role="tabpanel"
                             aria-label="<?php echo esc_attr($item['label']); ?>"
                             <?php echo $hidden ? 'hidden' : ''; ?>>
                        <div class="zerti-panel-body">
                            <h5 class="zerti-panel-title"><?php echo esc_html($panel_title); ?></h5>
                            <?php zerti_render_component_slot($slug, $current_tab); ?>
                        </div>
                    </section>
        <?php
    }
}

/**
 * Current server datetime for sidebar (plugin timezone + calendar setting).
 */
function zerti_format_server_datetime(): string
{
    return zerti_format_datetime(time(), true);
}

/**
 * Output user badge (name, role, server time) for sidebar / offcanvas.
 */
function zerti_render_user_badge(string $userLabel, string $roleLabel, string $serverTimeText): void
{
    $server_parts = zerti_split_datetime_display($serverTimeText);
    ?>
    <div class="zerti-user-badge">
        <strong><?php echo esc_html($userLabel); ?></strong>
        <?php if ($roleLabel !== '') : ?>
            <span class="zerti-user-role">
                <?php
                printf(
                    /* translators: %s: user role name */
                    esc_html__('Role: %s', 'zerti'),
                    esc_html($roleLabel)
                );
                ?>
            </span>
        <?php endif; ?>
        <span class="zerti-user-server-time">
            <?php
            echo wp_kses(
                sprintf(
                    /* translators: %s: Jalali server datetime (Persian digits). */
                    __('Server time: %s', 'zerti'),
                    '<span class="zerti-ltr-nums zerti-server-clock" dir="ltr" data-zerti-server-clock="1">' .
                    '<span class="zerti-server-clock__date">' . esc_html($server_parts['date']) . '</span>' .
                    '<span class="zerti-server-clock__time">' . esc_html($server_parts['time']) . '</span>' .
                    '</span>'
                ),
                [
                    'span' => [
                        'class' => true,
                        'dir' => true,
                        'data-zerti-server-clock' => true,
                    ],
                ]
            );
            ?>
        </span>
    </div>
    <?php
}

/**
 * Dark / light theme toggle for sidebar and offcanvas.
 */
function zerti_render_theme_switch(): void
{
    ?>
    <button type="button" class="zerti-theme-switch zerti-theme-switch--sidebar" data-zerti-theme-toggle
            role="switch"
            aria-checked="true"
            aria-label="<?php echo esc_attr__('Switch to light mode', 'zerti'); ?>"
            data-zerti-aria-dark="<?php echo esc_attr__('Switch to dark mode', 'zerti'); ?>"
            data-zerti-aria-light="<?php echo esc_attr__('Switch to light mode', 'zerti'); ?>">
        <i class="fa-solid fa-moon zerti-theme-switch-ico" aria-hidden="true"></i>
        <span class="zerti-theme-switch-track">
            <span class="zerti-theme-switch-thumb"></span>
        </span>
        <i class="fa-solid fa-sun zerti-theme-switch-ico" aria-hidden="true"></i>
    </button>
    <?php
}

/**
 * Sidebar / offcanvas edition label — rendered under the theme toggle.
 */
function zerti_render_admin_edition_footer(): void
{
    do_action('zerti_admin_edition_footer');
}

/**
 * zertiAdmin shell: top bar + main area on light gray.
 */
function zerti_render_admin_page(): void
{
    if (!zerti_user_can_access_admin()) {
        wp_die(
            esc_html__('You do not have permission to access the ZERTI panel.', 'zerti'),
            esc_html__('Access denied', 'zerti'),
            ['response' => 403]
        );
    }

    $user = wp_get_current_user();
    $userLabel = $user->exists() ? $user->display_name : __('User', 'zerti');
    $logo = zerti_logo_url();
    $current_tab = zerti_get_current_nav_tab();
    $serverTimeText = zerti_format_server_datetime();
    $roleLabel = '';
    if ($user->exists() && !empty($user->roles)) {
        $wp_roles = wp_roles();
        $names = [];
        foreach ($user->roles as $slug) {
            if (isset($wp_roles->roles[$slug]['name'])) {
                $names[] = translate_user_role($wp_roles->roles[$slug]['name']);
            }
        }
        $roleLabel = $names !== [] ? implode('، ', $names) : '';
    }
    $text_direction = function_exists('zerti_get_text_direction') ? zerti_get_text_direction() : 'rtl';
    $admin_html_lang = function_exists('zerti_admin_html_lang') ? zerti_admin_html_lang() : 'fa-IR';
    $digit_script = function_exists('zerti_get_digit_script') ? zerti_get_digit_script() : 'persian';
    $digits_format = function_exists('zerti_get_digits_format') ? zerti_get_digits_format() : 'auto';
    $visual_theme = function_exists('zerti_get_visual_theme') ? zerti_get_visual_theme() : 'zerti';
    ?>
    <div class="wrap"
         id="zertiAdmin"
         data-bs-theme="dark"
         data-zerti-visual-theme="<?php echo esc_attr($visual_theme); ?>"
         dir="<?php echo esc_attr($text_direction); ?>"
         lang="<?php echo esc_attr($admin_html_lang); ?>"
         data-zerti-digit-script="<?php echo esc_attr($digit_script); ?>"
         data-zerti-digits-format="<?php echo esc_attr($digits_format); ?>">
        <?php
        // Print theme boot in body to prevent dark/light FOUC; handle is registered/enqueued on admin_enqueue_scripts.
        if (wp_script_is('zerti-admin-theme-boot', 'enqueued')) {
            wp_print_scripts(['zerti-admin-theme-boot']);
        }
        ?>
        <?php
        if (function_exists('zerti_render_global_search_bar')) {
            zerti_render_global_search_bar();
        }
        ?>
        <div class="zerti-mobile-menu-trigger d-lg-none">
            <button class="zerti-mobile-menu-btn" type="button"
                    data-bs-toggle="offcanvas" data-bs-target="#zertiSidebar" aria-controls="zertiSidebar"
                    aria-label="<?php echo esc_attr__('Menu', 'zerti'); ?>">
                <i class="fa-solid fa-bars zerti-theme-switch-ico" aria-hidden="true"></i>
            </button>
        </div>
        <!-- Mobile sidebar (offcanvas) -->
        <div class="offcanvas zerti-sidebar-offcanvas" tabindex="-1" id="zertiSidebar" aria-labelledby="zertiSidebarLabel">
            <div class="offcanvas-header">
                <h5 class="offcanvas-title visually-hidden" id="zertiSidebarLabel"><?php esc_html_e('Menu', 'zerti'); ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="<?php esc_attr_e('Close', 'zerti'); ?>"></button>
            </div>
            <div class="offcanvas-body p-0 d-flex flex-column min-h-0">
                <div class="zerti-sidebar-branding pb-3 mb-3 border-bottom border-secondary-subtle text-center flex-shrink-0">
                    <div class="zerti-admin-branding">
                        <img class="zerti-admin-logo mx-auto" src="<?php echo esc_url($logo); ?>" alt="<?php echo esc_attr__('ZERTI Easy WooCommerce', 'zerti'); ?>" decoding="async" />
                    </div>
                </div>
                <div class="zerti-sidebar-nav-scroll flex-grow-1 min-h-0">
                    <?php zerti_render_admin_nav_pills($current_tab, 'offcanvas'); ?>
                </div>
                <div class="zerti-sidebar-theme">
                    <?php zerti_render_theme_switch(); ?>
                    <?php zerti_render_admin_edition_footer(); ?>
                    <?php zerti_render_admin_notes_toggle(); ?>
                </div>
                <div class="p-3 border-top d-grid gap-2 flex-shrink-0">
                    <?php zerti_render_user_badge($userLabel, $roleLabel, $serverTimeText); ?>
                    <div class="zerti-actions-row d-flex gap-2">
                        <a class="btn btn-primary flex-fill zerti-action-btn text-center" href="<?php echo esc_url(admin_url('index.php')); ?>">
                            <?php esc_html_e('Admin panel', 'zerti'); ?>
                        </a>
                        <a class="btn btn-outline-secondary flex-fill zerti-action-btn text-center" href="<?php echo esc_url(wp_logout_url(admin_url('index.php'))); ?>">
                            <?php esc_html_e('Logout', 'zerti'); ?>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="zerti-admin-main zerti-body">
            <!-- Desktop right sidebar -->
            <aside class="zerti-sidebar d-none d-lg-block">
                <div class="zerti-sidebar-inner">
                    <div class="zerti-sidebar-branding pb-3 mb-3 border-bottom border-secondary-subtle text-center">
                        <div class="zerti-admin-branding">
                            <img class="zerti-admin-logo mx-auto" src="<?php echo esc_url($logo); ?>" alt="<?php echo esc_attr__('ZERTI Easy WooCommerce', 'zerti'); ?>" decoding="async" />
                        </div>
                    </div>
                    <div class="zerti-sidebar-nav-scroll">
                        <?php zerti_render_admin_nav_pills($current_tab, 'sidebar'); ?>
                    </div>
                    <div class="zerti-sidebar-theme">
                        <?php zerti_render_theme_switch(); ?>
                        <?php zerti_render_admin_edition_footer(); ?>
                        <?php zerti_render_admin_notes_toggle(); ?>
                    </div>
                    <div class="zerti-sidebar-actions border-top d-grid gap-2">
                        <?php zerti_render_user_badge($userLabel, $roleLabel, $serverTimeText); ?>
                        <div class="zerti-actions-row d-flex gap-2">
                            <a class="btn btn-primary flex-fill zerti-action-btn text-center" href="<?php echo esc_url(admin_url('index.php')); ?>">
                                <?php esc_html_e('Admin panel', 'zerti'); ?>
                            </a>
                            <a class="btn btn-outline-secondary flex-fill zerti-action-btn text-center" href="<?php echo esc_url(wp_logout_url(admin_url('index.php'))); ?>">
                                <?php esc_html_e('Logout', 'zerti'); ?>
                            </a>
                        </div>
                    </div>
                </div>
            </aside>

            <main class="zerti-main-content">
                <div class="container-fluid p-0">
                    <div class="zerti-panels">
                        <?php zerti_render_admin_panels($current_tab); ?>
                    </div>
                </div>
            </main>
        </div>
        <?php zerti_render_admin_notes_panel(); ?>
    </div>
    <?php
}
