import importlib.util
import io
import os
import subprocess
import tempfile
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from unittest.mock import patch


def load_validate_workspace():
    module_path = Path(__file__).resolve().parent / "validate_workspace.py"
    spec = importlib.util.spec_from_file_location("validate_workspace", module_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


class ValidateWorkspaceTests(unittest.TestCase):
    def _script_command(self, validate_workspace, script_name: str) -> str:
        return validate_workspace.python_script_command(script_name)

    def test_env_commands_override_cmake_detection(self):
        validate_workspace = load_validate_workspace()
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            (root / "CMakeLists.txt").write_text("cmake_minimum_required(VERSION 3.20)\n", encoding="utf-8")
            with patch.dict(os.environ, {"HARNESS_VALIDATION_COMMANDS": "echo first\n echo second \n"}, clear=True):
                self.assertEqual(validate_workspace.discover_commands(root), ["echo first", "echo second"])

    def test_env_commands_override_abaqus_validation_config_in_main(self):
        validate_workspace = load_validate_workspace()
        env = {
            "HARNESS_VALIDATION_COMMANDS": "echo override",
            "HARNESS_ABAQUS_VALIDATION": "run",
        }
        with patch.dict(os.environ, env, clear=True):
            with patch.object(
                validate_workspace,
                "run_command",
                return_value=subprocess.CompletedProcess("echo override", 0, "", ""),
            ):
                with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()):
                    self.assertEqual(validate_workspace.main(), 0)

    def test_msvc_debug_cmake_commands_are_default_for_cmake_project(self):
        validate_workspace = load_validate_workspace()
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            (root / "CMakeLists.txt").write_text("cmake_minimum_required(VERSION 3.20)\n", encoding="utf-8")
            build_dir = root / "build" / "msvc-debug"
            with patch.dict(os.environ, {}, clear=True):
                self.assertEqual(
                    validate_workspace.discover_commands(root),
                    [
                        self._script_command(validate_workspace, "validate_reference_artifacts.py"),
                        self._script_command(validate_workspace, "validate_fortran.py"),
                        f'cmake -S "{root}" -B "{build_dir}" -G "Visual Studio 17 2022" -A x64',
                        f'cmake --build "{build_dir}" --config Debug',
                        f'ctest --test-dir "{build_dir}" --output-on-failure -C Debug',
                    ],
                )

    def test_msvc_debug_configure_preset_is_preferred_when_present(self):
        validate_workspace = load_validate_workspace()
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            (root / "CMakeLists.txt").write_text("cmake_minimum_required(VERSION 3.20)\n", encoding="utf-8")
            (root / "CMakePresets.json").write_text(
                """
{
  "version": 3,
  "configurePresets": [
    {
      "name": "msvc-debug",
      "generator": "Visual Studio 17 2022",
      "binaryDir": "${sourceDir}/out/msvc-debug"
    }
  ]
}
""",
                encoding="utf-8",
            )
            with patch.dict(os.environ, {}, clear=True):
                self.assertEqual(
                    validate_workspace.discover_commands(root),
                    [
                        self._script_command(validate_workspace, "validate_reference_artifacts.py"),
                        self._script_command(validate_workspace, "validate_fortran.py"),
                        "cmake --preset msvc-debug",
                        f'cmake --build "{root / "out" / "msvc-debug"}" --config Debug',
                        f'ctest --test-dir "{root / "out" / "msvc-debug"}" --output-on-failure -C Debug',
                    ],
                )

    def test_no_cmake_project_has_no_validation_commands(self):
        validate_workspace = load_validate_workspace()
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            with patch.dict(os.environ, {}, clear=True):
                self.assertEqual(
                    validate_workspace.discover_commands(root),
                    [
                        self._script_command(validate_workspace, "validate_reference_artifacts.py"),
                        self._script_command(validate_workspace, "validate_fortran.py"),
                    ],
                )

    def test_abaqus_detect_mode_reports_without_running_jobs(self):
        validate_workspace = load_validate_workspace()
        with patch.dict(os.environ, {"HARNESS_ABAQUS_VALIDATION": "detect"}, clear=True):
            validation = validate_workspace.discover_abaqus_validation(which=lambda _name: None)

        self.assertEqual(validation.mode, "detect")
        self.assertEqual(validation.commands, [])
        self.assertFalse(validation.required)

    def test_abaqus_run_mode_requires_executable_and_commands(self):
        validate_workspace = load_validate_workspace()
        with patch.dict(os.environ, {"HARNESS_ABAQUS_VALIDATION": "run"}, clear=True):
            with self.assertRaises(validate_workspace.ValidationConfigError):
                validate_workspace.discover_abaqus_validation(which=lambda _name: None)

    def test_abaqus_run_mode_replaces_abaqus_token(self):
        validate_workspace = load_validate_workspace()
        env = {
            "HARNESS_ABAQUS_VALIDATION": "run",
            "HARNESS_ABAQUS_VALIDATION_COMMANDS": "{abaqus} job=case user=UMAT.for",
            "HARNESS_ABAQUS_USE_ONEAPI_ENV": "off",
        }
        with patch.dict(os.environ, env, clear=True):
            validation = validate_workspace.discover_abaqus_validation(which=lambda _name: r"C:\SIMULIA\abaqus.bat")

        self.assertTrue(validation.required)
        self.assertEqual(validation.commands, [r"C:\SIMULIA\abaqus.bat job=case user=UMAT.for"])

    def test_common_cmake_install_path_is_prepended_when_cmake_is_not_on_path(self):
        validate_workspace = load_validate_workspace()
        with tempfile.TemporaryDirectory() as tmp:
            common_bin = Path(tmp) / "CMake" / "bin"
            common_bin.mkdir(parents=True)
            (common_bin / "cmake.exe").write_text("", encoding="utf-8")
            (common_bin / "ctest.exe").write_text("", encoding="utf-8")
            with patch.object(validate_workspace, "COMMON_CMAKE_BIN", common_bin):
                with patch.object(validate_workspace.shutil, "which", return_value=None):
                    env = validate_workspace.validation_environment({"PATH": "C:\\Windows\\System32"})

            self.assertTrue(env["PATH"].startswith(str(common_bin)))


if __name__ == "__main__":
    unittest.main()
