"""Fail-closed mutmut prepared-artifact reuse hooks for CI."""

from __future__ import annotations

import os


if (
    os.environ.get("OBLITERATUS_MUTMUT_REUSE_COVERAGE") == "1"
    or os.environ.get("OBLITERATUS_MUTMUT_REUSE_STATS") == "1"
    or os.environ.get("OBLITERATUS_MUTMUT_SUBPROCESS_PREFLIGHT") == "1"
):
    from mutmut import __main__ as mutmut_main

    from scripts.prepare_mutation_coverage import validate_execution_manifest
    from scripts.prepare_mutation_coverage import validate_manifest

    def store_lines_covered_by_tests_from_prepared_artifacts() -> None:
        validate_manifest()
        print("Reusing prepared mutmut covered-line artifacts")

    mutmut_main.store_lines_covered_by_tests = store_lines_covered_by_tests_from_prepared_artifacts

    if os.environ.get("OBLITERATUS_MUTMUT_REUSE_STATS") == "1":

        def collect_or_load_prepared_stats(
            runner,
            *,
            mutants_caught_by_type_checker=None,
            apply_config_invalidation=False,
            invalidate_stale_callers=True,
        ) -> None:
            del runner, mutants_caught_by_type_checker
            del apply_config_invalidation, invalidate_stale_callers
            validate_execution_manifest()
            if not mutmut_main.load_stats():
                raise RuntimeError("prepared mutmut stats failed to load")
            print("Reusing prepared mutmut test-selection stats")

        mutmut_main.collect_or_load_stats = collect_or_load_prepared_stats

    if os.environ.get("OBLITERATUS_MUTMUT_SUBPROCESS_PREFLIGHT") == "1":
        _original_execute_pytest = mutmut_main.PytestRunner.execute_pytest

        def execute_pytest_with_subprocess_preflight(self, params, **kwargs):
            if kwargs:
                return _original_execute_pytest(self, params, **kwargs)
            if os.environ.get("MUTANT_UNDER_TEST") not in {"", "fail"}:
                return _original_execute_pytest(self, params, **kwargs)

            import subprocess
            import sys

            full_params = ["--rootdir=.", "--tb=native", *params, *self._pytest_add_cli_args]
            result = subprocess.run([sys.executable, "-m", "pytest", *full_params], check=False)
            if result.returncode == 4:
                raise mutmut_main.BadTestExecutionCommandsException(full_params)
            return int(result.returncode)

        mutmut_main.PytestRunner.execute_pytest = execute_pytest_with_subprocess_preflight
