"""Shell layer: Sandbox dependency validation.

This module handles I/O operations for sandbox setup (checking if optional
dependencies are installed). These functions are used by Shell layer code
before executing sandboxed commands.

Shell zone: Returns Result[T, E] for error handling.
"""

from __future__ import annotations

import importlib
from returns.result import Result, Success, Failure


def check_srt_available() -> Result[None, str]:
    """Check if sandbox-runtime (srt) package is installed.

    This is an I/O operation (checking for optional dependency) and belongs
    in the Shell layer. Call this before executing wrapped sandbox commands
    if you want early failure with a clear error message.

    Returns:
        Success(None) if srt is installed.
        Failure(error_message) if srt is not installed.
    """
    try:
        importlib.import_module("srt")
        return Success(None)
    except ImportError:
        return Failure(
            "Sandbox requires sandbox-runtime package. "
            "Install with: pip install 'anima[sandbox]'"
        )


# @shell_orchestration: Thin adapter exposing exception-style shell API
def require_srt_available() -> None:
    """Check if sandbox-runtime (srt) package is installed, raise if not.

    Convenience function for Shell code that prefers exceptions over Result.

    Raises:
        ImportError: If sandbox-runtime is not installed.
    """
    result = check_srt_available()
    if isinstance(result, Failure):
        raise ImportError(result.failure())


__all__ = [
    "check_srt_available",
    "require_srt_available",
]
