"""Step implementations for decomposition_max_child_depth.feature.

Tests the enforcement of max_child_depth in DecompositionService._build_hierarchy.
Reuses existing steps from large_project_decomposition_steps.py where applicable.
Only new, unique step patterns are defined here.
"""

from __future__ import annotations

from typing import Any
from unittest.mock import patch

from behave import then, when  # type: ignore[import-untyped]

from cleveragents.application.services.decomposition_models import (
    DecompositionConfig,
)

# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------

_DECOMP_SVC_LOGGER = "cleveragents.application.services.decomposition_service.logger"


# ---------------------------------------------------------------------------
# Whens
# ---------------------------------------------------------------------------


@when("I decompose with max_child_depth {mcd:d} and max_depth {md:d}")
def step_when_decompose_max_child_depth_and_max_depth(
    context: Any,
    mcd: int,
    md: int,
) -> None:
    cfg = DecompositionConfig(
        max_child_depth=mcd,
        max_depth=md,
        max_files_per_subplan=30,
    )
    with patch(_DECOMP_SVC_LOGGER) as mock_logger:
        context.result = context.svc.decompose(context.files, cfg)
        context.mock_logger = mock_logger


@when("I decompose with default max_child_depth")
def step_when_decompose_default_mcd(context: Any) -> None:
    cfg = DecompositionConfig(
        max_child_depth=5,
        max_files_per_subplan=30,
    )
    with patch(_DECOMP_SVC_LOGGER) as mock_logger:
        context.result = context.svc.decompose(context.files, cfg)
        context.mock_logger = mock_logger


@when("I create a config with max_child_depth {n:d}")
def step_when_create_bad_max_child_depth(context: Any, n: int) -> None:
    try:
        DecompositionConfig(max_child_depth=n)
    except ValueError as exc:
        context.error = exc


# ---------------------------------------------------------------------------
# Thens
# ---------------------------------------------------------------------------


@then('the decomposition warning log should contain "{text}"')
def step_then_warning_log_contains(context: Any, text: str) -> None:
    assert context.mock_logger is not None, "Mock logger was not set"
    assert context.mock_logger.warning.called, "Expected logger.warning to be called"
    args_str = str(context.mock_logger.warning.call_args_list)
    assert text in args_str, f"Expected '{text}' in warning calls, got: {args_str}"


@then("max_child_depth should be {n:d}")
def step_then_cfg_max_child_depth(context: Any, n: int) -> None:
    assert context.decomp_config.max_child_depth == n
