<?php
/**
 * خانه — component partial (SSR + AJAX): WooCommerce sales snapshot.
 */
if (!defined('ABSPATH')) {
    exit;
}

$wc_ok = class_exists('WooCommerce') && function_exists('wc_get_orders');

/** Shell renders immediately; stats hydrate via zerti_ajax_home_dashboard_stats. */
$home_defer_stats = $wc_ok;

$placeholder_qty = '—';
$placeholder_price = '—';

$home_render_stat_value = static function (string $key, string $classes, bool $defer, ?string $format = null) use ($placeholder_qty, $placeholder_price): void {
    echo '<p class="' . esc_attr($classes) . '" data-zerti-home-stat="' . esc_attr($key) . '"'
        . ($format !== null ? ' data-zerti-home-stat-format="' . esc_attr($format) . '"' : '');
    if ($defer) {
        echo '>';
        echo '<span class="zerti-home-skeleton zerti-home-skeleton--value" aria-hidden="true"></span></p>';
        return;
    }
    echo '>';
    echo esc_html($format === 'html' ? $placeholder_price : $placeholder_qty);
    echo '</p>';
};

$processing_orders_url = '';
if ($wc_ok && function_exists('zerti_admin_route_url') && function_exists('zerti_user_can_access_tab') && zerti_user_can_access_tab('shop')) {
    $processing_orders_url = zerti_admin_route_url([
        'tab' => 'shop',
        'shop_status' => 'processing',
    ]);
}

$today_payments_url = '';
$period_payments_url = '';
$sales_report_period_url = '';
if ($wc_ok && function_exists('zerti_admin_route_url') && function_exists('zerti_user_can_access_tab') && zerti_user_can_access_tab('financial')) {
    $today_payments_url = zerti_admin_route_url([
        'tab' => 'financial',
        'payments_scope' => 'today_success',
    ]);
    $period_payments_url = zerti_admin_route_url([
        'tab' => 'financial',
        'payments_scope' => 'period_success',
    ]);
    $sales_report_period_url = zerti_admin_route_url([
        'tab' => 'financial',
        'fin' => 'sales-report',
        'sales_scope' => 'period',
    ]);
}

$zero_stock_products_url = '';
if (
    $wc_ok
    && function_exists('zerti_admin_route_url')
    && function_exists('zerti_user_can_access_tab')
    && zerti_user_can_access_tab('inventory')
) {
    $zero_stock_products_url = zerti_admin_route_url([
        'tab' => 'inventory',
        'inv' => 'warehouse',
        'products_stock' => 'zero',
    ]);
}

$one_stock_products_url = '';
if (
    $wc_ok
    && function_exists('zerti_admin_route_url')
    && function_exists('zerti_user_can_access_tab')
    && zerti_user_can_access_tab('inventory')
) {
    $one_stock_products_url = zerti_admin_route_url([
        'tab' => 'inventory',
        'inv' => 'warehouse',
        'products_stock' => 'one',
    ]);
}

$available_stock_products_url = '';
if (
    $wc_ok
    && function_exists('zerti_admin_route_url')
    && function_exists('zerti_user_can_access_tab')
    && zerti_user_can_access_tab('inventory')
) {
    $available_stock_products_url = zerti_admin_route_url([
        'tab' => 'inventory',
        'inv' => 'warehouse',
        'products_stock' => 'available',
    ]);
}

$home_leaderboards_ok = $wc_ok
    && function_exists('zerti_wc_analytics_leaderboards_available')
    && zerti_wc_analytics_leaderboards_available();
?>
<div class="zerti-component zerti-component-home<?php echo $home_defer_stats ? ' zerti-component-home--loading' : ''; ?>"<?php echo $home_defer_stats ? ' aria-busy="true"' : ''; ?>>
    <?php if (!$wc_ok) : ?>
        <p class="alert alert-warning mb-3" role="status">
            <?php esc_html_e('WooCommerce must be installed and active to view reports.', 'zerti'); ?>
        </p>
    <?php endif; ?>

    <div class="zerti-home-empty-slot-row">
        <?php if ($wc_ok) : ?>
            <div class="zerti-home-revenue-chart zerti-home-stat-card w-100" data-zerti-revenue-json="">
                <div class="zerti-home-revenue-chart__head small text-body-secondary mb-1">
                    <?php esc_html_e('Sales trend', 'zerti'); ?>
                </div>
                <div class="zerti-home-revenue-chart__wrap" dir="ltr">
                    <?php if ($home_defer_stats) : ?>
                        <div class="zerti-home-skeleton zerti-home-skeleton--chart" aria-hidden="true">
                            <?php
                            $chart_skeleton_heights = [38, 52, 45, 68, 55, 72, 48, 85, 62, 78, 58, 90, 66, 74, 50, 82];
                            foreach ($chart_skeleton_heights as $bar_height) :
                                ?>
                                <span
                                    class="zerti-home-skeleton__bar"
                                    style="--zerti-skeleton-h: <?php echo esc_attr((string) $bar_height); ?>%;"
                                ></span>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                    <canvas
                        class="zerti-home-revenue-chart__canvas"
                        role="img"
                        aria-label="<?php echo esc_attr__('Sales chart for last 30 days with Jalali dates', 'zerti'); ?>"
                        <?php echo $home_defer_stats ? ' hidden' : ''; ?>
                    ></canvas>
                </div>
            </div>
        <?php else : ?>
            <div class="zerti-home-revenue-chart zerti-home-stat-card w-100 zerti-home-revenue-chart--inactive">
                <p class="mb-0 small text-body-secondary text-center py-3">
                    <?php esc_html_e('WooCommerce must be installed and active to view the chart.', 'zerti'); ?>
                </p>
            </div>
        <?php endif; ?>
    </div>

    <div class="row row-cols-1 row-cols-md-2 row-cols-xl-4 g-3 zerti-home-stat-grid">
        <div class="col">
            <?php if ($processing_orders_url !== '') : ?>
                <a
                    href="<?php echo esc_url($processing_orders_url); ?>"
                    class="zerti-home-stat-card zerti-home-stat-card--link"
                    data-zerti-tab="shop"
                    data-zerti-shop-status="processing"
                >
            <?php else : ?>
                <div class="zerti-home-stat-card">
            <?php endif; ?>
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Order count — processing', 'zerti'); ?></p>
                <?php $home_render_stat_value('processing_orders', 'zerti-home-stat-value zerti-ltr-nums mb-0', $home_defer_stats); ?>
            <?php echo $processing_orders_url !== '' ? '</a>' : '</div>'; ?>
        </div>
        <div class="col">
            <?php if ($today_payments_url !== '') : ?>
                <a
                    href="<?php echo esc_url($today_payments_url); ?>"
                    class="zerti-home-stat-card zerti-home-stat-card--link"
                    data-zerti-tab="financial"
                    data-zerti-fin-sub="payments"
                    data-zerti-payments-scope="today_success"
                >
            <?php else : ?>
                <div class="zerti-home-stat-card">
            <?php endif; ?>
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Sales amount — today', 'zerti'); ?></p>
                <?php $home_render_stat_value('today_total', 'zerti-home-stat-value zerti-home-stat-price mb-0', $home_defer_stats, 'html'); ?>
            <?php echo $today_payments_url !== '' ? '</a>' : '</div>'; ?>
        </div>
        <div class="col">
            <?php if ($sales_report_period_url !== '') : ?>
                <a
                    href="<?php echo esc_url($sales_report_period_url); ?>"
                    class="zerti-home-stat-card zerti-home-stat-card--link"
                    data-zerti-tab="financial"
                    data-zerti-fin-sub="sales-report"
                    data-zerti-sales-scope="period"
                >
            <?php else : ?>
                <div class="zerti-home-stat-card">
            <?php endif; ?>
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Units sold — current period', 'zerti'); ?></p>
                <?php $home_render_stat_value('period_qty', 'zerti-home-stat-value zerti-ltr-nums mb-0', $home_defer_stats); ?>
            <?php echo $sales_report_period_url !== '' ? '</a>' : '</div>'; ?>
        </div>
        <div class="col">
            <?php if ($period_payments_url !== '') : ?>
                <a
                    href="<?php echo esc_url($period_payments_url); ?>"
                    class="zerti-home-stat-card zerti-home-stat-card--link"
                    data-zerti-tab="financial"
                    data-zerti-fin-sub="payments"
                    data-zerti-payments-scope="period_success"
                >
            <?php else : ?>
                <div class="zerti-home-stat-card">
            <?php endif; ?>
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Sales amount — current period', 'zerti'); ?></p>
                <?php $home_render_stat_value('period_total', 'zerti-home-stat-value zerti-home-stat-price mb-0', $home_defer_stats, 'html'); ?>
            <?php echo $period_payments_url !== '' ? '</a>' : '</div>'; ?>
        </div>
    </div>

    <div class="row row-cols-1 row-cols-md-2 row-cols-xl-4 g-3 zerti-home-stat-grid">
        <div class="col">
            <?php if ($available_stock_products_url !== '') : ?>
                <a
                    href="<?php echo esc_url($available_stock_products_url); ?>"
                    class="zerti-home-stat-card zerti-home-stat-card--link"
                    data-zerti-tab="inventory"
                    data-zerti-inv-sub="warehouse"
                    data-zerti-products-stock="available"
                >
            <?php else : ?>
                <div class="zerti-home-stat-card">
            <?php endif; ?>
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Items in stock', 'zerti'); ?></p>
                <?php $home_render_stat_value('available_count', 'zerti-home-stat-value zerti-ltr-nums mb-0', $home_defer_stats); ?>
            <?php echo $available_stock_products_url !== '' ? '</a>' : '</div>'; ?>
        </div>
        <div class="col">
            <?php if ($one_stock_products_url !== '') : ?>
                <a
                    href="<?php echo esc_url($one_stock_products_url); ?>"
                    class="zerti-home-stat-card zerti-home-stat-card--link"
                    data-zerti-tab="inventory"
                    data-zerti-inv-sub="warehouse"
                    data-zerti-products-stock="one"
                >
            <?php else : ?>
                <div class="zerti-home-stat-card">
            <?php endif; ?>
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Items with stock of one', 'zerti'); ?></p>
                <?php $home_render_stat_value('near_zero_count', 'zerti-home-stat-value zerti-ltr-nums mb-0', $home_defer_stats); ?>
            <?php echo $one_stock_products_url !== '' ? '</a>' : '</div>'; ?>
        </div>
        <div class="col">
            <?php if ($zero_stock_products_url !== '') : ?>
                <a
                    href="<?php echo esc_url($zero_stock_products_url); ?>"
                    class="zerti-home-stat-card zerti-home-stat-card--link"
                    data-zerti-tab="inventory"
                    data-zerti-inv-sub="warehouse"
                    data-zerti-products-stock="zero"
                >
            <?php else : ?>
                <div class="zerti-home-stat-card">
            <?php endif; ?>
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Items with zero stock', 'zerti'); ?></p>
                <?php $home_render_stat_value('zero_count', 'zerti-home-stat-value zerti-ltr-nums mb-0', $home_defer_stats); ?>
            <?php echo $zero_stock_products_url !== '' ? '</a>' : '</div>'; ?>
        </div>
        <div class="col">
            <div class="zerti-home-stat-card">
                <p class="zerti-home-stat-label text-body-secondary mb-0"><?php esc_html_e('Total value of stock on hand', 'zerti'); ?></p>
                <?php $home_render_stat_value('available_value', 'zerti-home-stat-value zerti-home-stat-price mb-0', $home_defer_stats, 'html'); ?>
            </div>
        </div>
    </div>

    <?php if ($home_leaderboards_ok) : ?>
        <div class="row g-3 zerti-home-leaderboards-row">
            <?php
            $home_leaderboard_defs = [
                'categories' => __('Top categories - Items sold', 'zerti'),
                'products' => __('Top products - Items sold', 'zerti'),
            ];
            foreach ($home_leaderboard_defs as $board_id => $board_title) :
                ?>
                <div class="col-12 col-lg-6">
                    <div
                        class="zerti-home-leaderboard-card zerti-home-stat-card"
                        data-zerti-home-leaderboard="<?php echo esc_attr($board_id); ?>"
                    >
                        <p class="zerti-home-stat-label text-body-secondary mb-0 zerti-home-leaderboard-card__title" data-zerti-home-leaderboard-title>
                            <?php echo esc_html($board_title); ?>
                        </p>
                        <div class="zerti-home-leaderboard-card__body" data-zerti-home-leaderboard-body>
                            <?php if ($home_defer_stats) : ?>
                                <div class="zerti-home-skeleton zerti-home-skeleton--leaderboard" aria-hidden="true">
                                    <?php for ($sk = 0; $sk < 5; $sk++) : ?>
                                        <span class="zerti-home-skeleton__leaderboard-row"></span>
                                    <?php endfor; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
