from __future__ import annotations

from pathlib import Path


FORMAL_SOURCE_ROOTS = [
    Path("app"),
    Path("pipeline"),
    Path("acquisition"),
    Path("decode_content"),
    Path("core"),
]

LEGACY_RUNTIME_TOKENS = [
    "CK_SQLITE_PATH",
    "data/app.db",
    "/data/queries",
    "/api/creation-search",
    "creation_knowledge.api",
    "from acquisition import store",
    "import acquisition.store",
    "from creation_knowledge",
    "import creation_knowledge",
]

LEGACY_QUERY_TOKENS = [
    "from acquisition.query",
    "import acquisition.query",
    "tactic2_form_llm",
    "tactic3_suggest",
    "tactic4_llm",
    "tactic_multiaxis",
    "tactic1",
    "tactic2",
    "tactic3",
    "tactic4",
    "build_query_demo",
    "build_family6",
    "query_gen.txt",
    "form_query_gen.txt",
    "data/queries/demo.json",
]


def _active_source_files():
    for root in FORMAL_SOURCE_ROOTS:
        for path in root.rglob("*"):
            if path.is_file() and path.suffix in {".py", ".js", ".jsx", ".ts", ".tsx"}:
                yield path


def test_formal_sources_do_not_depend_on_archived_legacy_runtime():
    offenders: list[str] = []
    for path in _active_source_files():
        text = path.read_text(encoding="utf-8")
        for token in LEGACY_RUNTIME_TOKENS:
            if token in text:
                offenders.append(f"{path}:{token}")

    assert offenders == []


def test_formal_sources_do_not_depend_on_legacy_query_tactics():
    offenders: list[str] = []
    script_roots = [Path("scripts")]
    for root in FORMAL_SOURCE_ROOTS + script_roots:
        for path in root.rglob("*"):
            if path.is_file() and path.suffix in {".py", ".js", ".jsx", ".ts", ".tsx"}:
                text = path.read_text(encoding="utf-8")
                for token in LEGACY_QUERY_TOKENS:
                    if token in text:
                        offenders.append(f"{path}:{token}")

    assert offenders == []


def test_legacy_debug_decompose_entry_is_explicitly_labeled():
    text = Path("scripts/decompose.py").read_text(encoding="utf-8")

    assert "LEGACY/debug" in text[:500]
    assert "正式云端 pipeline 入口" in text[:500]
    assert "web/frameworks" in text
    assert "web/payloads" in text
