"""Step definitions for ``project_context_depth_gradient.feature``.

Tests the ``--depth-gradient`` flag on ``agents project context set``
and its display in ``agents project context show``.
"""

from __future__ import annotations

from typing import Any

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

# ------------------------------------------------------------------
# Helpers — reuse the shared infrastructure from
# project_context_cli_steps (background steps, _run_with_container,
# _mock_container, etc. are already registered).
# ------------------------------------------------------------------


def _run_with_container_local(
    context: Any, func: Any, *args: Any, **kwargs: Any
) -> None:
    """Delegate to the shared test runner from project_context_cli_steps."""
    from features.steps.project_context_cli_steps import (
        _run_with_container,
    )

    _run_with_container(context, func, *args, **kwargs)


# ------------------------------------------------------------------
# When steps
# ------------------------------------------------------------------


@when(
    'I run context set on "{project}" with depth-gradient "{gradient}"',
)
def step_ctx_set_depth_gradient_single(
    context: Any, project: str, gradient: str
) -> None:
    from cleveragents.cli.commands.project_context import (
        context_set,
    )

    _run_with_container_local(
        context,
        context_set,
        project=project,
        view="default",
        depth_gradient=[gradient],
    )


@when(
    'I run context set on "{project}" with depth-gradients "{gradients}"',
)
def step_ctx_set_depth_gradient_multi(
    context: Any, project: str, gradients: str
) -> None:
    from cleveragents.cli.commands.project_context import (
        context_set,
    )

    gradient_list = [g.strip() for g in gradients.split(",")]
    _run_with_container_local(
        context,
        context_set,
        project=project,
        view="default",
        depth_gradient=gradient_list,
    )


# ------------------------------------------------------------------
# Given steps
# ------------------------------------------------------------------


@given('I have set depth-gradient "{gradients}" on "{project}"')
def step_given_depth_gradient(context: Any, gradients: str, project: str) -> None:
    from cleveragents.cli.commands.project_context import (
        context_set,
    )

    gradient_list = [g.strip() for g in gradients.split(",")]
    _run_with_container_local(
        context,
        context_set,
        project=project,
        view="default",
        depth_gradient=gradient_list,
    )


# ------------------------------------------------------------------
# Then steps
# ------------------------------------------------------------------


def _read_stored_acms(context: Any) -> dict[str, Any]:
    """Read the stored ACMS config from the test DB."""
    from cleveragents.cli.commands.project_context import (
        _read_acms_config,
    )

    return _read_acms_config(context.ctx_session_factory, "local/ctx-app")


@then('the stored ACMS depth gradient should have hop "{hop}" with value {value:d}')
def step_depth_gradient_int(context: Any, hop: str, value: int) -> None:
    acms = _read_stored_acms(context)
    dg = acms.get("depth_gradient", {})
    assert hop in dg, f"Hop '{hop}' not in depth_gradient: {dg}"
    assert dg[hop] == value, f"Expected {value} for hop {hop}, got {dg[hop]}"


@then(
    'the stored ACMS depth gradient should have hop "{hop}" with string value "{value}"'
)
def step_depth_gradient_str(context: Any, hop: str, value: str) -> None:
    acms = _read_stored_acms(context)
    dg = acms.get("depth_gradient", {})
    assert hop in dg, f"Hop '{hop}' not in depth_gradient: {dg}"
    assert dg[hop] == value, f"Expected '{value}' for hop {hop}, got {dg[hop]}"


@then('the context show output should contain "{text}"')
def step_show_output_contains(context: Any, text: str) -> None:
    assert text in context.ctx_output, (
        f"Expected '{text}' in output, got: {context.ctx_output!r}"
    )
