"""Step definitions for a2a_acp_module_removed.feature.

Tests that the old ACP module is completely removed and not importable,
ensuring v3.6.0 deliverable #1 compliance: "No acp references in public API".
"""

from __future__ import annotations

import importlib
import sys
from pathlib import Path
from typing import Any

from behave import then, when


@when("I attempt to import the cleveragents.acp module")
def step_attempt_import_acp(context: Any) -> None:
    """Attempt to import cleveragents.acp and capture any ImportError."""
    context.import_error = None
    try:
        # Remove from sys.modules if it exists to ensure fresh import
        if "cleveragents.acp" in sys.modules:
            del sys.modules["cleveragents.acp"]

        # Attempt to import
        importlib.import_module("cleveragents.acp")
        context.import_succeeded = True
    except ImportError as e:
        context.import_error = e
        context.import_succeeded = False


@then("an ImportError should be raised")
def step_check_import_error(context: Any) -> None:
    """Verify that an ImportError was raised."""
    assert context.import_error is not None, (
        "Expected ImportError to be raised when importing cleveragents.acp, "
        "but import succeeded"
    )
    assert not context.import_succeeded, "Expected import to fail, but it succeeded"


@then("the error message should indicate the module does not exist")
def step_check_error_message(context: Any) -> None:
    """Verify that the error message indicates the module doesn't exist."""
    error_msg = str(context.import_error)
    assert "cleveragents.acp" in error_msg or "No module named" in error_msg, (
        f"Expected error message to mention 'cleveragents.acp' or 'No module named', "
        f"but got: {error_msg}"
    )


@when("I check the source tree for the acp directory")
def step_check_acp_directory(context: Any) -> None:
    """Check if the acp directory exists in the source tree."""
    # Get the path to the cleveragents package
    import cleveragents

    cleveragents_path = Path(cleveragents.__file__).parent
    acp_path = cleveragents_path / "acp"

    context.acp_directory_exists = acp_path.exists()
    context.acp_path = acp_path


@then("the src/cleveragents/acp directory should not exist")
def step_check_acp_not_exists(context: Any) -> None:
    """Verify that the acp directory does not exist."""
    assert not context.acp_directory_exists, (
        f"Expected acp directory to not exist, but found it at: {context.acp_path}"
    )
