"""Robot Framework helper for execution environment routing tests."""

from __future__ import annotations

import importlib
import sys
from pathlib import Path

# Ensure local source tree is importable
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

import cleveragents  # noqa: E402

importlib.reload(cleveragents)

from cleveragents.application.services.execution_environment_resolver import (  # noqa: E402
    CONTAINER_RESOURCE_TYPES,
    ContainerUnavailableError,
    ExecutionEnvironmentResolver,
)
from cleveragents.domain.models.core.plan import ExecutionEnvironment  # noqa: E402


def _run_resolve_default() -> None:
    """Resolve with no overrides => host."""
    resolver = ExecutionEnvironmentResolver()
    result = resolver.resolve()
    assert result == ExecutionEnvironment.HOST
    print("resolve-default-ok")


def _run_resolve_priority() -> None:
    """Verify priority chain."""
    resolver = ExecutionEnvironmentResolver()

    # project overrides default
    assert resolver.resolve(project_env="container") == ExecutionEnvironment.CONTAINER

    # plan overrides project
    assert (
        resolver.resolve(plan_env="host", project_env="container")
        == ExecutionEnvironment.HOST
    )

    # tool overrides plan
    assert (
        resolver.resolve(tool_env="container", plan_env="host")
        == ExecutionEnvironment.CONTAINER
    )
    print("resolve-priority-ok")


def _run_container_validation() -> None:
    """Validate container availability checks."""
    resolver = ExecutionEnvironmentResolver()

    # Should pass with container resource
    assert resolver.validate_container_available(["devcontainer-instance"]) is True

    # Should raise without container resource
    try:
        resolver.validate_container_available(["git-checkout"])
        print("FAIL: expected ContainerUnavailableError")
        sys.exit(1)
    except ContainerUnavailableError:
        pass

    print("container-validation-ok")


def _run_enum_values() -> None:
    """Verify enum values."""
    assert ExecutionEnvironment.HOST.value == "host"
    assert ExecutionEnvironment.CONTAINER.value == "container"
    print("enum-values-ok")


def _run_resource_types() -> None:
    """Verify container resource types."""
    assert "container-instance" in CONTAINER_RESOURCE_TYPES
    assert "devcontainer-instance" in CONTAINER_RESOURCE_TYPES
    assert "devcontainer-file" in CONTAINER_RESOURCE_TYPES
    assert "git-checkout" not in CONTAINER_RESOURCE_TYPES
    print("resource-types-ok")


if __name__ == "__main__":
    cmd = sys.argv[1] if len(sys.argv) > 1 else "all"
    dispatch = {
        "resolve-default": _run_resolve_default,
        "resolve-priority": _run_resolve_priority,
        "container-validation": _run_container_validation,
        "enum-values": _run_enum_values,
        "resource-types": _run_resource_types,
    }

    if cmd == "all":
        for fn in dispatch.values():
            fn()
    elif cmd in dispatch:
        dispatch[cmd]()
    else:
        print(f"Unknown command: {cmd}")
        sys.exit(1)
