"""P45 Dead Code Cleanup — verify archived/deleted files are gone from original locations,
and verify all 10 P45 orphan modules are no longer importable (D61-D70).
"""
import os
import importlib
import pytest

REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def _path(*parts):
    return os.path.join(REPO_ROOT, *parts)


# ── D71-D75: Retroactive scripts archived ─────────────────────────────────────

def test_d71_clean_wants_archived():
    assert not os.path.exists(_path("pulse", "admin", "retroactive", "_clean_wants.py"))

def test_d72_clean_intents_batch1_archived():
    assert not os.path.exists(_path("pulse", "admin", "retroactive", "clean_intents_batch1.py"))

def test_d73_finish_batches_archived():
    assert not os.path.exists(_path("pulse", "admin", "retroactive", "finish_batches.py"))

def test_d74_retroactive_sign_archived():
    assert not os.path.exists(_path("pulse", "admin", "retroactive", "retroactive_sign.py"))

def test_d75_backfill_session_descriptions_archived():
    assert not os.path.exists(_path("pulse", "admin", "retroactive", "backfill_session_descriptions.py"))

# ── D76-D80: Setup scripts archived ───────────────────────────────────────────

def test_d76_generate_admin_keys_archived():
    assert not os.path.exists(_path("pulse", "admin", "setup", "generate_admin_keys.py"))

def test_d77_generate_anchors_archived():
    assert not os.path.exists(_path("pulse", "admin", "setup", "generate_anchors.py"))

def test_d78_migrate_to_sqlite_archived():
    assert not os.path.exists(_path("pulse", "admin", "setup", "migrate_to_sqlite.py"))

def test_d79_migrate_remaining_json_archived():
    assert not os.path.exists(_path("pulse", "admin", "setup", "migrate_remaining_json.py"))

def test_d80_hippocampus_wiring_archived():
    assert not os.path.exists(_path("pulse", "admin", "setup", "hippocampus_wiring.py"))

# ── D81-D84: Stale MD-era brain_ops files deleted ─────────────────────────────

def test_d81_synthesizer_deleted():
    assert not os.path.exists(_path("pulse", "admin", "brain_ops", "synthesizer.py"))

def test_d82_growth_monitor_deleted():
    assert not os.path.exists(_path("pulse", "admin", "brain_ops", "growth_monitor.py"))

def test_d83_refactor_evidence_deleted():
    assert not os.path.exists(_path("pulse", "admin", "brain_ops", "refactor_evidence.py"))

def test_d84_brain_cleanse_deleted():
    assert not os.path.exists(_path("pulse", "admin", "brain_ops", "brain_cleanse.py"))

# ── Archive directories exist with expected files ─────────────────────────────

def test_retroactive_archive_dir_exists():
    assert os.path.isdir(_path("pulse", "admin", "retroactive", "archive"))

def test_setup_archive_dir_exists():
    assert os.path.isdir(_path("pulse", "admin", "setup", "archive"))

def test_retroactive_archive_contains_files():
    archive = _path("pulse", "admin", "retroactive", "archive")
    py_files = [f for f in os.listdir(archive) if f.endswith(".py") and f != "__init__.py"]
    assert len(py_files) == 5  # D71-D75

def test_setup_archive_contains_files():
    archive = _path("pulse", "admin", "setup", "archive")
    py_files = [f for f in os.listdir(archive) if f.endswith(".py") and f != "__init__.py"]
    assert len(py_files) == 5  # D76-D80

# ── pulse package still imports cleanly ───────────────────────────────────────

def test_pulse_import_still_works():
    import pulse  # noqa: F401


# ── D61-D70: Orphan modules deleted (must raise ImportError) ──────────────────

_D61_D70_DELETED_MODULES = [
    "pulse.brain.knowledge_redundancy",   # D61
    "pulse.brain.knowledge_coherence",    # D62
    "pulse.brain.agent_trust_evolution",  # D63
    "pulse.brain.predictive_search",      # D64
    "pulse.core.error_attribution",       # D65
    "pulse.core.pattern_yield",           # D66
    "pulse.comms.dissent",                # D67
    "pulse.admin.pulse_consensus",        # D68
    "pulse.admin.tool_dependency_graph",  # D69
    "pulse.core.config",                  # D70
]


@pytest.mark.parametrize("module_name", _D61_D70_DELETED_MODULES)
def test_d61_d70_deleted_module_not_importable(module_name):
    """Each P45 orphan module must raise ImportError — confirms deletion is complete."""
    with pytest.raises(ImportError):
        importlib.import_module(module_name)
