import hashlib
import importlib.machinery
import importlib.util
import io
import json
import os
from pathlib import Path
import subprocess
import tarfile
import tempfile
import unittest
from contextlib import redirect_stdout
from unittest import mock


SCRIPT_DIR = Path(__file__).resolve().parent
RUNNER_PATH = SCRIPT_DIR / "nodedc-deploy"
BUILDER_PATH = (
    SCRIPT_DIR
    / "build-engine-mcp-gelios-units-items-envelope-artifact.mjs"
)
ENGINE_ROOT = SCRIPT_DIR.parent.parent.parent / "NODEDC_ENGINE_INFRA"
PATCH_ID = "engine-mcp-gelios-units-items-envelope-20991231-999"


def load_runner():
    loader = importlib.machinery.SourceFileLoader(
        "nodedc_engine_mcp_gelios_units_items",
        str(RUNNER_PATH),
    )
    spec = importlib.util.spec_from_loader(loader.name, loader)
    module = importlib.util.module_from_spec(spec)
    loader.exec_module(module)
    return module


RUNNER = load_runner()


class EngineMcpGeliosUnitsItemsEnvelopeTest(unittest.TestCase):
    def build(self, artifact_dir):
        environment = os.environ.copy()
        environment["NODEDC_ENGINE_SOURCE_ROOT"] = str(ENGINE_ROOT)
        environment["NODEDC_DEPLOY_ARTIFACT_DIR"] = str(artifact_dir)
        completed = subprocess.run(
            ["node", str(BUILDER_PATH), PATCH_ID],
            check=True,
            capture_output=True,
            text=True,
            env=environment,
        )
        return json.loads(completed.stdout)

    def test_builder_emits_exact_deterministic_catalog_slice(self):
        with tempfile.TemporaryDirectory(
            prefix="nodedc-engine-gelios-units-items-builder-"
        ) as directory:
            root = Path(directory)
            first = self.build(root / "first")
            second = self.build(root / "second")
            first_artifact = Path(first["artifact"])
            second_artifact = Path(second["artifact"])
            self.assertEqual(
                first_artifact.read_bytes(),
                second_artifact.read_bytes(),
            )
            self.assertEqual(
                first["sha256"],
                hashlib.sha256(first_artifact.read_bytes()).hexdigest(),
            )
            self.assertEqual(first["services"], ["nodedc-backend"])
            self.assertEqual(first["mcpVersion"], "0.12.0")
            self.assertEqual(
                first["registeredProfile"],
                "gelios.units.profile.cold.v1",
            )
            self.assertEqual(first["responseCollectionPath"], "items")
            self.assertEqual(first["existingMaterializerReused"], True)
            self.assertEqual(first["engineRuntimeCodeChanged"], False)
            self.assertEqual(first["n8nCoreChanged"], False)
            self.assertEqual(first["l2GraphChanged"], False)

            extract = root / "extract"
            with tarfile.open(first_artifact, "r:gz") as archive:
                archive.extractall(extract, filter="data")
            entries = tuple(
                (extract / "files.txt").read_text(encoding="utf-8").splitlines()
            )
            self.assertEqual(
                entries,
                RUNNER.ENGINE_MCP_GELIOS_UNITS_ITEMS_ARTIFACT_ENTRIES,
            )
            self.assertTrue(
                RUNNER.is_engine_mcp_gelios_units_items_slice(
                    "engine",
                    entries,
                )
            )
            self.assertEqual(
                RUNNER.component_services("engine", entries),
                ("nodedc-backend",),
            )
            RUNNER.validate_engine_mcp_gelios_units_items_slice(
                extract / "payload",
                entries,
            )

    def test_preflight_requires_exact_registered_profiles_predecessor(self):
        with tempfile.TemporaryDirectory(
            prefix="nodedc-engine-gelios-units-items-preflight-"
        ) as directory:
            root = Path(directory)
            all_live = {
                **RUNNER.ENGINE_MCP_GELIOS_UNITS_ITEMS_PREDECESSOR_SHA256,
                **RUNNER.ENGINE_MCP_GELIOS_UNITS_ITEMS_FOUNDATION_SHA256,
            }
            for relative_path in all_live:
                path = root / relative_path
                path.parent.mkdir(parents=True, exist_ok=True)
                path.write_text("live\n", encoding="utf-8")

            def exact_hash(path):
                relative_path = Path(path).relative_to(root).as_posix()
                return all_live[relative_path]

            with (
                mock.patch.object(RUNNER, "component_root", return_value=root),
                mock.patch.object(RUNNER, "sha256_file", side_effect=exact_hash),
                mock.patch.object(
                    RUNNER,
                    "preflight_engine_credential_backend_runtime",
                    return_value={"mode": "verified-derived-retry"},
                ),
            ):
                result = (
                    RUNNER
                    .preflight_engine_mcp_gelios_units_items_predecessor()
                )
            self.assertEqual(
                result["mode"],
                (
                    "registered-profiles-v2-to-"
                    "gelios-v12.0.1-units-items-envelope"
                ),
            )
            self.assertEqual(
                result["new_paths"],
                RUNNER.ENGINE_MCP_GELIOS_UNITS_ITEMS_NEW_PATHS,
            )

            changed = next(iter(
                RUNNER.ENGINE_MCP_GELIOS_UNITS_ITEMS_PREDECESSOR_SHA256
            ))
            with (
                mock.patch.object(RUNNER, "component_root", return_value=root),
                mock.patch.object(
                    RUNNER,
                    "sha256_file",
                    side_effect=lambda path: (
                        "0" * 64
                        if Path(path).relative_to(root).as_posix() == changed
                        else exact_hash(path)
                    ),
                ),
            ):
                with self.assertRaises(RUNNER.DeployError):
                    (
                        RUNNER
                        .preflight_engine_mcp_gelios_units_items_predecessor()
                    )

    def test_plan_renders_exact_provider_package_patch_boundary(self):
        with tempfile.TemporaryDirectory(
            prefix="nodedc-engine-gelios-units-items-plan-"
        ) as directory:
            root = Path(directory)
            built = self.build(root / "artifacts")
            artifact = Path(built["artifact"])
            live_root = root / "live"
            live_root.mkdir()
            preflight = {
                "mode": (
                    "registered-profiles-v2-to-"
                    "gelios-v12.0.1-units-items-envelope"
                ),
                "predecessor_sha256": dict(
                    RUNNER
                    .ENGINE_MCP_GELIOS_UNITS_ITEMS_PREDECESSOR_SHA256
                ),
                "foundation_sha256": dict(
                    RUNNER
                    .ENGINE_MCP_GELIOS_UNITS_ITEMS_FOUNDATION_SHA256
                ),
                "target_sha256": dict(
                    RUNNER.ENGINE_MCP_GELIOS_UNITS_ITEMS_TARGET_SHA256
                ),
                "new_paths": (
                    RUNNER.ENGINE_MCP_GELIOS_UNITS_ITEMS_NEW_PATHS
                ),
                "backend_mode": "verified-derived-retry",
            }
            output = io.StringIO()
            with (
                mock.patch.object(RUNNER, "validate_artifact_location"),
                mock.patch.object(RUNNER, "ensure_layout"),
                mock.patch.object(RUNNER, "TMP_DIR", root),
                mock.patch.object(
                    RUNNER,
                    "component_root",
                    return_value=live_root,
                ),
                mock.patch.object(
                    RUNNER,
                    "component_compose_root",
                    return_value=live_root,
                ),
                mock.patch.object(
                    RUNNER,
                    "preflight_engine_mcp_gelios_units_items_predecessor",
                    return_value=preflight,
                ),
                mock.patch.object(
                    RUNNER,
                    "preflight_engine_credential_backend_runtime",
                    return_value={"mode": "verified-derived-retry"},
                ),
                mock.patch.object(RUNNER, "state_has_sha", return_value=False),
                mock.patch.object(
                    RUNNER,
                    "state_has_patch_id",
                    return_value=False,
                ),
                redirect_stdout(output),
            ):
                RUNNER.plan_artifact(artifact)
            plan = output.getvalue()
            self.assertIn("services=nodedc-backend", plan)
            self.assertIn("engine_mcp_version=0.12.0", plan)
            self.assertIn(
                "engine_mcp_provider_package=gelios.provider.v12@12.0.1",
                plan,
            )
            self.assertIn(
                "engine_mcp_registered_profile=gelios.units.profile.cold.v1",
                plan,
            )
            self.assertIn("engine_mcp_response_collection_path=items", plan)
            self.assertIn("engine_mcp_existing_materializer=reused", plan)
            self.assertIn("engine_mcp_runtime_code_changed=no", plan)
            self.assertIn("engine_mcp_provider_endpoints_changed=no", plan)
            self.assertIn("l2_graph=untouched", plan)
            self.assertIn("n8n_core=untouched", plan)
            self.assertIn("foundry=untouched", plan)
            self.assertIn("state=new", plan)

    def test_live_acceptance_probes_catalog_and_tools(self):
        expected_live = (
            "engine-mcp-gelios-units-items:0.12.0:"
            "gelios.provider.v12@12.0.1:technical-profile:items"
        )
        with (
            mock.patch.object(RUNNER, "component_root", return_value=ENGINE_ROOT),
            mock.patch.object(
                RUNNER,
                "engine_backend_container_id",
                return_value="backend",
            ),
            mock.patch.object(
                RUNNER,
                "run_engine_backend_probe",
                return_value=expected_live,
            ) as backend_probe,
        ):
            result = RUNNER.accept_engine_mcp_gelios_units_items_runtime()
        self.assertEqual(result["live"], expected_live)
        probe_source = backend_probe.call_args.args[0][-1]
        self.assertIn("engine_list_l2_execution_profiles", probe_source)
        self.assertIn("engine_plan_registered_l2_execution", probe_source)
        self.assertIn("gelios.units.profile.cold.v1", probe_source)
        self.assertIn("collectionPaths?.[0]!=='items'", probe_source)


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