"""Isolated tests for the CI-only mutmut covered-line reuse hook."""

from __future__ import annotations

import os
from pathlib import Path
import subprocess
import sys


ROOT = Path(__file__).parents[1]
SITECUSTOMIZE = ROOT / "scripts" / "mutmut_coverage_sitecustomize"


def _write_fake_modules(tmp_path: Path, *, validation: str = "return None") -> None:
    mutmut_dir = tmp_path / "mutmut"
    scripts_dir = tmp_path / "scripts"
    mutmut_dir.mkdir()
    scripts_dir.mkdir()
    (mutmut_dir / "__init__.py").write_text("", encoding="utf-8")
    (mutmut_dir / "__main__.py").write_text(
        "def store_lines_covered_by_tests():\n"
        "    print('original coverage collector')\n",
        encoding="utf-8",
    )
    (scripts_dir / "__init__.py").write_text("", encoding="utf-8")
    (scripts_dir / "prepare_mutation_coverage.py").write_text(
        "def validate_manifest():\n"
        f"    {validation}\n"
        "def validate_execution_manifest():\n"
        f"    {validation}\n",
        encoding="utf-8",
    )


def _run_hook_probe(tmp_path: Path, *, env_flag: bool, validation: str = "return None"):
    _write_fake_modules(tmp_path, validation=validation)
    env = os.environ.copy()
    env.pop("OBLITERATUS_MUTMUT_REUSE_COVERAGE", None)
    env["PYTHONPATH"] = f"{SITECUSTOMIZE}:{tmp_path}"
    if env_flag:
        env["OBLITERATUS_MUTMUT_REUSE_COVERAGE"] = "1"
    return subprocess.run(
        [
            sys.executable,
            "-c",
            "from mutmut import __main__ as m; m.store_lines_covered_by_tests()",
        ],
        check=False,
        env=env,
        text=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )


def test_sitecustomize_does_not_patch_without_reuse_env(tmp_path):
    result = _run_hook_probe(tmp_path, env_flag=False)

    assert result.returncode == 0
    assert result.stdout.strip() == "original coverage collector"


def test_sitecustomize_patches_only_with_reuse_env_and_valid_manifest(tmp_path):
    result = _run_hook_probe(tmp_path, env_flag=True, validation="print('validated')")

    assert result.returncode == 0
    assert "validated" in result.stdout
    assert "Reusing prepared mutmut covered-line artifacts" in result.stdout
    assert "original coverage collector" not in result.stdout


def test_sitecustomize_fails_closed_when_manifest_validation_fails(tmp_path):
    result = _run_hook_probe(
        tmp_path,
        env_flag=True,
        validation="raise RuntimeError('stale manifest')",
    )

    assert result.returncode != 0
    assert "stale manifest" in result.stderr


def test_sitecustomize_reuses_prepared_stats_when_enabled(tmp_path):
    _write_fake_modules(tmp_path, validation="print('validated execution')")
    env = os.environ.copy()
    env.pop("OBLITERATUS_MUTMUT_REUSE_COVERAGE", None)
    env["OBLITERATUS_MUTMUT_REUSE_STATS"] = "1"
    env["PYTHONPATH"] = f"{SITECUSTOMIZE}:{tmp_path}"

    result = subprocess.run(
        [
            sys.executable,
            "-c",
            (
                "from mutmut import __main__ as m\n"
                "m.load_stats = lambda: True\n"
                "m.collect_or_load_stats(object())\n"
            ),
        ],
        check=False,
        env=env,
        text=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    assert result.returncode == 0
    assert "validated execution" in result.stdout
    assert "Reusing prepared mutmut test-selection stats" in result.stdout
