"""Simple steps to verify MCP logger code exists and can execute."""

from typing import Any

from behave import given, then, when

import cleveragents.cli.commands.session as session_module


@given("the session CLI modules are loaded")
def step_modules_loaded(context: Any) -> None:
    """Ensure modules are loaded."""
    context.session_module = session_module


@when("I inspect the session create function source")
def step_inspect_create_source(context: Any) -> None:
    """Get the session create function source code."""
    import inspect

    context.create_source = inspect.getsource(session_module.create)


@when("I inspect the session list function source")
def step_inspect_list_source(context: Any) -> None:
    """Get the session list function source code."""
    import inspect

    context.list_source = inspect.getsource(session_module.list_sessions)


@then("the source should contain mcp_logger initialization")
def step_contains_logger_init(context: Any) -> None:
    """Verify source contains MCP logger initialization."""
    source = (
        context.create_source
        if hasattr(context, "create_source")
        else context.list_source
    )
    assert "mcp_logger = logging.getLogger" in source, (
        "Source should contain mcp_logger initialization"
    )


@then("the source should contain logging.getLogger call")
def step_contains_getLogger(context: Any) -> None:
    """Verify source contains getLogger call."""
    source = (
        context.create_source
        if hasattr(context, "create_source")
        else context.list_source
    )
    assert "logging.getLogger(_MCP_LOGGER_NAME)" in source, (
        "Source should contain logging.getLogger call"
    )


@then("the source should contain format check for RICH and COLOR")
def step_contains_format_check(context: Any) -> None:
    """Verify source contains format check."""
    source = (
        context.create_source
        if hasattr(context, "create_source")
        else context.list_source
    )
    assert (
        "OutputFormat.RICH.value" in source and "OutputFormat.COLOR.value" in source
    ), "Source should contain format check for RICH and COLOR"


@then("the source should contain setLevel CRITICAL")
def step_contains_setLevel_critical(context: Any) -> None:
    """Verify source contains setLevel CRITICAL."""
    source = (
        context.create_source
        if hasattr(context, "create_source")
        else context.list_source
    )
    assert "mcp_logger.setLevel(logging.CRITICAL)" in source, (
        "Source should contain setLevel CRITICAL"
    )


@then("the source should contain finally block")
def step_contains_finally(context: Any) -> None:
    """Verify source contains finally block."""
    source = (
        context.create_source
        if hasattr(context, "create_source")
        else context.list_source
    )
    assert "finally:" in source, "Source should contain finally block"


@then("the source should contain mcp_logger.setLevel(orig_level)")
def step_contains_restore(context: Any) -> None:
    """Verify source contains logger restoration."""
    source = (
        context.create_source
        if hasattr(context, "create_source")
        else context.list_source
    )
    assert "mcp_logger.setLevel(orig_level)" in source, (
        "Source should contain mcp_logger.setLevel(orig_level)"
    )
