{#
    EwoooC Sidebar v3.0 — Production
    ─────────────────────────────────────────────────────────────
    變更重點：
      1. 移除原本 200 行 inline <style>（背景漸層、點陣 mask、subtree 漸層）
      2. 群組 accent 由 ewoooc_base.html 透過 [data-page-group] 統一管理
      3. 結構簡化：nav-link 不再有 box-shadow / transform translate
      4. 保留所有真實連結與 Jinja 條件邏輯，工程師可以直接覆蓋
#}

{% set _active_page = active_page|default('') %}
{% set _is_growth_command = _active_page == 'ai_intelligence' %}
{% set _is_price_workbench = _active_page == 'dashboard' and request is defined and request.args.get('filter') == 'pchome_review' %}
{% set _next_run = next_run|default(None) %}
{% set _session_username = session.get('username') if session is defined else None %}
{% set _session_role = session.get('role') if session is defined else None %}
{% set _is_logged_in = session.get('logged_in') if session is defined else false %}
{% set _analysis_pages = ['sales', 'daily_sales', 'monthly', 'growth', 'metabase', 'grist'] %}
{% set _obs_pages = [
    'obs_overview', 'obs_agent_orchestration', 'obs_business_intel',
    'obs_host_health', 'obs_ai_calls', 'obs_budget',
    'obs_promotion_review', 'obs_rag_queries', 'obs_quality_trend',
    'obs_ppt_audit'
] %}

{% set _scheduler = scheduler_stats|default({}) %}
{% set _momo_runs = _scheduler.get('momo_task', []) if _scheduler is mapping else [] %}
{% set _latest_run = _momo_runs[0] if _momo_runs else {} %}
{% set _has_scheduler_data = _latest_run is mapping and _latest_run %}
{% set _last_run = _latest_run.get('last_run', '--') if _has_scheduler_data else '--' %}
{% set _scanned = _latest_run.get('scanned_count', _latest_run.get('total_products', '--')) if _latest_run is mapping else '--' %}
{% set _added = _latest_run.get('new_records', _latest_run.get('added', '--')) if _latest_run is mapping else '--' %}
{% set _run_status = _latest_run.get('status', '') if _has_scheduler_data else '' %}
{% set _status_label = '尚無紀錄' if not _has_scheduler_data else ('最近成功' if _run_status in ['Success', 'success', 'SUCCESS'] else (_run_status or '已有紀錄')) %}

<aside class="momo-sidebar" aria-label="主選單">
    <a class="momo-sidebar-logo" href="/">
        <span class="momo-logo-mark" aria-hidden="true">
            <span></span><span></span><span></span>
            <span></span><span></span><span></span>
            <span></span><span></span><span></span>
        </span>
        <span class="momo-brand-word">
            <span class="momo-brand-name">EwoooC</span>
        </span>
    </a>

    <nav class="momo-nav momo-scroll">
        {# 群組 1: 作戰（caramel） #}
        <div class="momo-nav-group">
            <div class="momo-nav-group-title">作戰</div>
            <a class="momo-nav-link {% if _is_growth_command %}is-active{% endif %}" href="/">
                <span class="momo-nav-icon"><i class="fas fa-chart-line"></i></span>
                <span class="momo-nav-label">業績成長指揮台</span>
                <span class="momo-nav-code">01</span>
            </a>
            <a class="momo-nav-link {% if _is_price_workbench %}is-active{% endif %}" href="/dashboard?filter=pchome_review&review_status=all&sort_by=pchome_review&order=desc">
                <span class="momo-nav-icon"><i class="fas fa-scale-balanced"></i></span>
                <span class="momo-nav-label">比價工作台</span>
                <span class="momo-nav-code">02</span>
            </a>
            <a class="momo-nav-link {% if _active_page == 'dashboard' and not _is_price_workbench %}is-active{% endif %}" href="/dashboard">
                <span class="momo-nav-icon"><i class="fas fa-border-all"></i></span>
                <span class="momo-nav-label">舊商品看板</span>
                <span class="momo-nav-code">舊</span>
            </a>
            <a class="momo-nav-link {% if _active_page in ['edm', 'campaigns'] %}is-active{% endif %}" href="/edm">
                <span class="momo-nav-icon"><i class="fas fa-bullhorn"></i></span>
                <span class="momo-nav-label">活動看板</span>
                <span class="momo-nav-code">03</span>
            </a>
        </div>

        {# 群組 2: 分析（honey） #}
        <div class="momo-nav-group">
            <div class="momo-nav-group-title">分析</div>
            <details class="momo-nav-tree" {% if _active_page in _analysis_pages %}open{% endif %}>
                <summary class="momo-nav-link momo-nav-tree-summary {% if _active_page in _analysis_pages %}is-active{% endif %}">
                    <span class="momo-nav-icon"><i class="fas fa-chart-line"></i></span>
                    <span class="momo-nav-label">分析報表</span>
                    <span class="momo-nav-code">03</span>
                </summary>
                <div class="momo-nav-subtree">
                    <a class="momo-nav-sublink {% if _active_page == 'sales' %}is-active{% endif %}" href="/sales_analysis">
                        <i class="fas fa-chart-bar"></i><span>業績分析</span><span class="momo-nav-code">01</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'daily_sales' %}is-active{% endif %}" href="/daily_sales">
                        <i class="fas fa-calendar-day"></i><span>當日業績</span><span class="momo-nav-code">02</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'growth' %}is-active{% endif %}" href="/growth_analysis">
                        <i class="fas fa-arrow-trend-up"></i><span>成長分析</span><span class="momo-nav-code">03</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'monthly' %}is-active{% endif %}" href="/monthly_summary_analysis">
                        <i class="fas fa-table"></i><span>月份總表</span><span class="momo-nav-code">04</span>
                    </a>
                    {% if metabase_url %}
                    <a class="momo-nav-sublink {% if _active_page == 'metabase' %}is-active{% endif %}" href="{{ metabase_url }}">
                        <i class="fas fa-chart-pie"></i><span>自訂圖表</span><span class="momo-nav-code">外部</span>
                    </a>
                    {% endif %}
                    {% if grist_url %}
                    <a class="momo-nav-sublink {% if _active_page == 'grist' %}is-active{% endif %}" href="{{ grist_url }}">
                        <i class="fas fa-table-cells"></i><span>資料協作</span><span class="momo-nav-code">外部</span>
                    </a>
                    {% endif %}
                </div>
            </details>
        </div>

        {# 群組 3: 營運（clay） #}
        <div class="momo-nav-group">
            <div class="momo-nav-group-title">營運</div>
            <a class="momo-nav-link {% if _active_page == 'vendor_stockout' %}is-active{% endif %}" href="/vendor-stockout">
                <span class="momo-nav-icon"><i class="fas fa-box-open"></i></span>
                <span class="momo-nav-label">廠商缺貨</span>
                <span class="momo-nav-code">04</span>
            </a>
            <a class="momo-nav-link {% if _active_page == 'auto_import' %}is-active{% endif %}" href="/auto_import">
                <span class="momo-nav-icon"><i class="fas fa-download"></i></span>
                <span class="momo-nav-label">雲端匯入</span>
                <span class="momo-nav-code">06</span>
            </a>
        </div>

        {# 群組 4: AI 中樞（saffron） #}
        <div class="momo-nav-group">
            <div class="momo-nav-group-title">AI 中樞</div>
            <a class="momo-nav-link {% if _active_page in ['ai_recommend', 'ai_history', 'pchome_crawler', 'price_comparison', 'trends'] %}is-active{% endif %}" href="/ai_recommend">
                <span class="momo-nav-icon"><i class="fas fa-wand-magic-sparkles"></i></span>
                <span class="momo-nav-label">AI 助手</span>
                <span class="momo-nav-code">05</span>
            </a>
            <details class="momo-nav-tree" {% if _active_page in _obs_pages %}open{% endif %}>
                <summary class="momo-nav-link momo-nav-tree-summary {% if _active_page in _obs_pages %}is-active{% endif %}">
                    <span class="momo-nav-icon"><i class="fas fa-satellite-dish"></i></span>
                    <span class="momo-nav-label">AI 觀測台</span>
                    <span class="momo-nav-code">07</span>
                </summary>
                <div class="momo-nav-subtree">
                    <div class="momo-nav-subtitle">戰情室</div>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_overview' %}is-active{% endif %}" href="/observability/overview">
                        <i class="fas fa-gauge-high"></i><span>觀測台總覽</span><span class="momo-nav-code">01</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_agent_orchestration' %}is-active{% endif %}" href="/observability/agent_orchestration">
                        <i class="fas fa-network-wired"></i><span>AI 分工矩陣</span><span class="momo-nav-code">02</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_business_intel' %}is-active{% endif %}" href="/observability/business_intel">
                        <i class="fas fa-briefcase"></i><span>商業面 × AI</span><span class="momo-nav-code">03</span>
                    </a>
                    <div class="momo-nav-subtitle">系統與成本</div>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_host_health' %}is-active{% endif %}" href="/observability/host_health">
                        <i class="fas fa-heartbeat"></i><span>主機健康</span><span class="momo-nav-code">04</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_ai_calls' %}is-active{% endif %}" href="/observability/ai_calls">
                        <i class="fas fa-chart-bar"></i><span>AI 呼叫</span><span class="momo-nav-code">05</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_budget' %}is-active{% endif %}" href="/observability/budget">
                        <i class="fas fa-wallet"></i><span>預算控管</span><span class="momo-nav-code">06</span>
                    </a>
                    <div class="momo-nav-subtitle">知識與品質</div>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_promotion_review' %}is-active{% endif %}" href="/observability/promotion_review">
                        <i class="fas fa-brain"></i><span>知識晉升審核</span><span class="momo-nav-code">07</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_rag_queries' %}is-active{% endif %}" href="/observability/rag_queries">
                        <i class="fas fa-magnifying-glass-chart"></i><span>知識召回詳情</span><span class="momo-nav-code">08</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_quality_trend' %}is-active{% endif %}" href="/observability/quality_trend">
                        <i class="fas fa-comments"></i><span>反饋趨勢</span><span class="momo-nav-code">09</span>
                    </a>
                    <a class="momo-nav-sublink {% if _active_page == 'obs_ppt_audit' %}is-active{% endif %}" href="/observability/ppt_audit_history">
                        <i class="fas fa-search"></i><span>PPT 視覺審核</span><span class="momo-nav-code">10</span>
                    </a>
                </div>
            </details>
        </div>

        {# 群組 5: 系統（terra） #}
        <div class="momo-nav-group">
            <div class="momo-nav-group-title">系統</div>
            <a class="momo-nav-link {% if _active_page in ['settings', 'system_settings', 'logs', 'crawler', 'user_management', 'login_history', 'change_password', 'notification_templates', 'ai_automation_smoke', 'brand_assets', 'code_review', 'cicd'] %}is-active{% endif %}" href="/settings">
                <span class="momo-nav-icon"><i class="fas fa-gear"></i></span>
                <span class="momo-nav-label">系統管理</span>
                <span class="momo-nav-code">14</span>
            </a>
            <a class="momo-nav-link {% if _active_page == 'webcrumbs' %}is-active{% endif %}" href="/webcrumbs">
                <span class="momo-nav-icon"><i class="fas fa-cubes"></i></span>
                <span class="momo-nav-label">Webcrumbs</span>
                <span class="momo-nav-code">UI</span>
            </a>
        </div>
    </nav>

    <div class="momo-status-card">
        <div class="momo-status-title">商品監控狀態</div>
        <div class="momo-status-active">
            <span class="momo-live-dot"></span>
            <span>{{ _status_label }}</span>
        </div>
        <div class="momo-status-meta">
            上次 {{ _last_run }}<br>
            掃描 {{ _scanned }}<br>
            新增 {% if _added == '--' %}--{% else %}+{{ _added }}{% endif %}
        </div>
    </div>
</aside>

<div class="momo-shell-backdrop" data-momo-sidebar-close></div>
