from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def _template() -> str:
    return (ROOT / "templates/ai_intelligence.html").read_text(encoding="utf-8")


def _css_block(template: str, selector: str) -> str:
    marker = f"{selector} {{"
    assert marker in template
    return template.split(marker, 1)[1].split("}", 1)[0]


def _any_css_block_has(template: str, selector: str, expected: str) -> bool:
    marker = f"{selector} {{"
    assert marker in template
    return any(
        expected in block.split("}", 1)[0]
        for block in template.split(marker)[1:]
    )


def test_ai_intelligence_first_viewport_is_compact_workbench():
    template = _template()
    command_shell = template.split('<section class="growth-command-pro"', 1)[1].split(
        '<section class="ai-intel-hero',
        1,
    )[0]

    assert 'data-density-guardrail="compact-ai-workbench"' in command_shell
    assert "growth-command-mode" in command_shell
    assert "AI 作戰" in command_shell
    assert "證據優先" in command_shell
    assert "下一步" in command_shell
    assert "growth-command-pro-subtitle" not in command_shell
    assert "先看今日優先商品，再決定價格、曝光、組合與資料補強" not in command_shell
    assert "正在整理今天第一件事" not in command_shell
    assert "讀取 PChome 業績與 MOMO 比價" not in command_shell


def test_ai_intelligence_hides_explanatory_copy_layers():
    template = _template()
    hidden_selectors = [
        ".growth-command-kpi-note",
        ".growth-command-mini-card em",
        ".growth-command-alert span",
        ".ai-benchmark-detail",
        ".growth-strategy-note",
        ".growth-category-note",
        ".growth-playbook-note",
        ".growth-playbook-product,\n    .growth-playbook-action",
        ".growth-decision-copy",
        ".growth-product-reason-list",
        ".growth-action-hint",
        ".growth-data-summary",
        ".growth-detail-line + .growth-detail-line",
        ".growth-source-detail",
    ]

    for selector in hidden_selectors:
        assert _any_css_block_has(template, selector, "display: none")


def test_ai_intelligence_runtime_task_copy_stays_short():
    template = _template()
    command_renderer = template.split("function renderGrowthCommandCenter", 1)[1].split(
        "function renderGrowthCommandTopDecliners",
        1,
    )[0]

    for phrase in [
        "今天先做：",
        "先確認最新業績已匯入",
        "確認同款後才進價格判斷",
        "先補 MOMO 對應，再判斷價格",
        "先看 MOMO 價格較低的商品",
    ]:
        assert phrase not in command_renderer

    for short_task in ["更新業績", "確認候選", "補比價", "檢查價格壓力"]:
        assert short_task in command_renderer


def test_ai_intelligence_comparison_coverage_has_explicit_scope_and_partition():
    template = _template()
    renderer = template.split("function renderGrowthCommandCenter", 1)[1].split(
        "function renderGrowthCommandTopDecliners",
        1,
    )[0]

    assert "同款比價證據 · TOP 50" in template
    assert "正式資料平台" in template
    assert "比價覆蓋 · TOP 50" in template
    assert "具正式比價證據" in template
    assert "候選 / 來源驗證" in template
    assert "尚未找到同款" in template
    assert "比價可用率" not in template
    assert "needsMapping + reviewCandidateCount" not in renderer
    assert "commandComparisonReady" in renderer
    assert "commandCandidateValidation" in renderer
    assert "commandUnmatched" in renderer


def test_ai_intelligence_detail_meta_and_single_product_avoid_long_reasons():
    template = _template()
    detail_config = template.split("function growthDetailConfig", 1)[1].split(
        "function growthDetailRows",
        1,
    )[0]
    detail_renderer = template.split("function renderGrowthDetail", 1)[1].split(
        "function renderGrowthOps",
        1,
    )[0]
    product_detail = template.split("function showGrowthProductDetail", 1)[1].split(
        "function classifyGrowthRow",
        1,
    )[0]

    for phrase in [
        "依優先級排序",
        "MOMO 參考價較低",
        "PChome 目前較有價格優勢",
        "需要檢查組合包",
        "只列出已接到 MOMO 外部參考價",
    ]:
        assert phrase not in detail_config

    assert "${subtitle}" not in detail_renderer
    assert "先列" not in detail_renderer
    assert "· 顯示 ${visibleRows.length.toLocaleString()}" in detail_renderer
    assert "growth-product-reason-list" not in product_detail
    assert "reason_lines" not in product_detail
