"""Step definitions for RemoteGraphConfig scenarios in langgraph_platform_remote_graph.feature."""

from __future__ import annotations

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

from cleveragents.langgraph.remote_graph import (
    RemoteGraphConfig,
)

# ---------------------------------------------------------------------------
# RemoteGraphConfig steps
# ---------------------------------------------------------------------------


@when(
    'I create a basic RemoteGraphConfig with graph_id "{graph_id}" and platform_url "{platform_url}"'
)
def step_create_basic_remote_graph_config(
    context: Context, graph_id: str, platform_url: str
) -> None:
    context.remote_graph_config = RemoteGraphConfig(
        graph_id=graph_id,
        platform_url=platform_url,
    )


@when('I create a RemoteGraphConfig with custom api_key_env "{api_key_env}"')
def step_create_remote_graph_config_with_api_key(
    context: Context, api_key_env: str
) -> None:
    context.remote_graph_config = RemoteGraphConfig(
        graph_id="actor",
        platform_url="https://langgraph.example.com",
        api_key_env=api_key_env,
    )


@when("I create a RemoteGraphConfig with custom timeout {timeout:f}")
def step_create_remote_graph_config_with_timeout(
    context: Context, timeout: float
) -> None:
    context.remote_graph_config = RemoteGraphConfig(
        graph_id="actor",
        platform_url="https://langgraph.example.com",
        timeout=timeout,
    )


@then('the config graph_id should be "{expected}"')
def step_config_graph_id(context: Context, expected: str) -> None:
    assert context.remote_graph_config.graph_id == expected, (
        f"Expected graph_id={expected!r}, got {context.remote_graph_config.graph_id!r}"
    )


@then('the config platform_url should be "{expected}"')
def step_config_platform_url(context: Context, expected: str) -> None:
    assert context.remote_graph_config.platform_url == expected, (
        f"Expected platform_url={expected!r}, "
        f"got {context.remote_graph_config.platform_url!r}"
    )


@then('the config api_key_env should be "{expected}"')
def step_config_api_key_env(context: Context, expected: str) -> None:
    assert context.remote_graph_config.api_key_env == expected, (
        f"Expected api_key_env={expected!r}, "
        f"got {context.remote_graph_config.api_key_env!r}"
    )


@then("the config timeout should be {expected:f}")
def step_config_timeout(context: Context, expected: float) -> None:
    assert context.remote_graph_config.timeout == expected, (
        f"Expected timeout={expected}, got {context.remote_graph_config.timeout}"
    )


@when("I try to create a RemoteGraphConfig with empty graph_id")
def step_try_create_config_empty_graph_id(context: Context) -> None:
    try:
        RemoteGraphConfig(graph_id="", platform_url="https://langgraph.example.com")
        context.remote_graph_config_error = None
    except Exception as exc:
        context.remote_graph_config_error = exc


@when("I try to create a RemoteGraphConfig with empty platform_url")
def step_try_create_config_empty_platform_url(context: Context) -> None:
    try:
        RemoteGraphConfig(graph_id="actor", platform_url="")
        context.remote_graph_config_error = None
    except Exception as exc:
        context.remote_graph_config_error = exc


@when('I try to create a RemoteGraphConfig with platform_url "{bad_url}"')
def step_try_create_config_bad_platform_url(context: Context, bad_url: str) -> None:
    try:
        RemoteGraphConfig(graph_id="actor", platform_url=bad_url)
        context.remote_graph_config_error = None
    except Exception as exc:
        context.remote_graph_config_error = exc


@when("I try to create a RemoteGraphConfig with timeout {timeout:d}")
def step_try_create_config_bad_timeout(context: Context, timeout: int) -> None:
    try:
        RemoteGraphConfig(
            graph_id="actor",
            platform_url="https://langgraph.example.com",
            timeout=float(timeout),
        )
        context.remote_graph_config_error = None
    except Exception as exc:
        context.remote_graph_config_error = exc


@then("a remote graph config validation error should be raised")
def step_remote_graph_config_validation_error(context: Context) -> None:
    assert context.remote_graph_config_error is not None, (
        "Expected a validation error but none was raised"
    )


@then("the remote graph config should be immutable")
def step_remote_graph_config_immutable(context: Context) -> None:
    try:
        context.remote_graph_config.graph_id = "mutated"  # type: ignore[misc]
        raise AssertionError("Expected immutability error but assignment succeeded")
    except AssertionError:
        raise
    except Exception:
        pass  # Expected — frozen model
