"""축 D(죽은 무게 정리) — 죽은 프롬프트 표식이 실제 죽음과 맞는지 검증.

표식만 달아두면 나중에 배선이 되살아나도 표식이 남아 사람을 속인다. 그래서
두 방향을 같이 잰다: ①표식이 있는가 ②정말 호출자가 없는가. 둘 중 하나가
어긋나면 실패 — 다시 배선했으면 표식을 지우라는 뜻이다.
"""
from __future__ import annotations

from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[3]
PROVIDER = (
    REPO_ROOT / "backend" / "app" / "modules" / "pipeline"
    / "floor_plan_layout_plan_provider.py"
)
DEAD_MARK = "DEAD (2026-08-15 확인): 호출자 없음, 축 D 정리 대상"

# 호출자를 찾을 범위 — 실행 경로가 될 수 있는 모든 파이썬 소스.
SEARCH_ROOTS = (
    REPO_ROOT / "backend" / "app",
    REPO_ROOT / "backend" / "tests",
    REPO_ROOT / "scripts",
    REPO_ROOT / "tools",
)


def _python_sources():
    for root in SEARCH_ROOTS:
        if not root.is_dir():
            continue
        for path in root.rglob("*.py"):
            if ".venv" in path.parts:
                continue
            yield path


def test_layout_plan_provider_carries_dead_marker():
    text = PROVIDER.read_text(encoding="utf-8")
    assert DEAD_MARK in text, "모듈 docstring 의 DEAD 표식이 사라졌다"
    assert text.count(DEAD_MARK) >= 2, (
        "_SYSTEM_PROMPT 바로 위 표식이 사라졌다 (docstring 만으로는 "
        "프롬프트를 읽는 사람이 못 본다)"
    )


def test_layout_plan_provider_still_has_no_callers():
    """표식의 근거 재검 — 호출자가 생겼으면 표식부터 지워야 한다."""
    offenders = []
    for path in _python_sources():
        if path == PROVIDER or path == Path(__file__).resolve():
            continue
        text = path.read_text(encoding="utf-8", errors="ignore")
        if ("litellm_layout_plan_provider" in text
                or "floor_plan_layout_plan_provider" in text):
            offenders.append(str(path.relative_to(REPO_ROOT)))
    assert not offenders, (
        "floor_plan_layout_plan_provider 를 참조하는 코드가 생겼다 — "
        f"DEAD 표식을 제거하라: {offenders}"
    )
