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 test_ai_intelligence_detail_rows_keep_four_price_evidence_cells():
    template = _template()
    detail_renderer = template.split("function renderGrowthDetail", 1)[1].split(
        "function renderGrowthOps",
        1,
    )[0]

    assert 'aria-label="價格證據"' in detail_renderer
    assert 'data-evidence="pchome-price"' in detail_renderer
    assert 'data-evidence="momo-reference"' in detail_renderer
    assert 'data-evidence="price-gap"' in detail_renderer
    assert 'data-evidence="confidence"' in detail_renderer
    assert '<span class="growth-price-label">PChome</span>' in detail_renderer
    assert '<span class="growth-price-label">MOMO</span>' in detail_renderer
    assert '<span class="growth-price-label">差距</span>' in detail_renderer
    assert '<span class="growth-price-label">可信度</span>' in detail_renderer
    assert "候選待確認" in detail_renderer
    assert "待補" in detail_renderer


def test_ai_intelligence_single_product_detail_keeps_same_evidence_contract():
    template = _template()
    product_detail = template.split("function showGrowthProductDetail", 1)[1].split(
        "function classifyGrowthRow",
        1,
    )[0]

    assert 'class="growth-product-evidence-grid" aria-label="價格證據"' in product_detail
    assert "productEvidenceTile('PChome'" in product_detail
    assert "productEvidenceTile('MOMO'" in product_detail
    assert "productEvidenceTile('差距'" in product_detail
    assert "productEvidenceTile('可信度'" in product_detail
    assert "'pchome-price'" in product_detail
    assert "'momo-reference'" in product_detail
    assert "'price-gap'" in product_detail
    assert "'confidence'" in product_detail


def test_ai_intelligence_detail_controls_cover_search_sort_and_momo_candidates():
    template = _template()
    detail_panel = template.split('aria-label="商品明細工具"', 1)[1].split(
        '<div class="growth-product-panel',
        1,
    )[0]
    row_filter = template.split("function growthDetailRows", 1)[1].split(
        "function showGrowthDetail",
        1,
    )[0]

    assert "商品、分類、編號、MOMO候選" in detail_panel
    for option in ["priority", "sales", "gap", "decline", "quality"]:
        assert f'value="{option}"' in detail_panel
    assert "candidate.momo_name" in row_filter
    assert "candidate.momo_sku" in row_filter
