from __future__ import annotations

import ast
from typing import Any
from unittest.mock import MagicMock, patch

from behave import given, then, when

from cleveragents.providers.llm.openrouter_provider import OpenRouterChatProvider


def _register_cleanup(context, cleanup):
    if hasattr(context, "add_cleanup"):
        context.add_cleanup(cleanup)
    else:
        cleanup_handlers = getattr(context, "_cleanup_handlers", [])
        cleanup_handlers.append(cleanup)
        context._cleanup_handlers = cleanup_handlers


def _parse_kwargs_string(kwargs_string: str) -> dict[str, Any]:
    if not kwargs_string:
        return {}
    parsed: dict[str, Any] = {}
    for entry in kwargs_string.split(","):
        entry = entry.strip()
        if not entry:
            continue
        if "=" not in entry:
            parsed[entry] = True
            continue
        key, value = entry.split("=", 1)
        key = key.strip()
        value = value.strip()
        try:
            parsed_value = ast.literal_eval(value)
        except Exception:
            parsed_value = value
        parsed[key] = parsed_value
    return parsed


def _parse_headers_string(headers_string: str) -> dict[str, str]:
    if not headers_string:
        return {}
    parsed: dict[str, str] = {}
    for entry in headers_string.split(","):
        entry = entry.strip()
        if not entry:
            continue
        if "=" not in entry:
            parsed[entry] = ""
            continue
        key, value = entry.split("=", 1)
        parsed[key.strip()] = value.strip()
    return parsed


def _setup_plan_generation_graph(context) -> MagicMock:
    patcher = patch(
        "cleveragents.providers.llm.langchain_chat_provider.PlanGenerationGraph"
    )
    context.plan_generation_patcher = patcher
    mock_graph_class = patcher.start()
    _register_cleanup(context, patcher.stop)
    mock_graph_instance = MagicMock(name="PlanGenerationGraphInstance")

    default_state = {
        "generated_changes": [],
        "validation_result": {"status": "PASS"},
        "error": None,
    }
    state_override = getattr(context, "plan_generation_state_override", None)
    mock_graph_instance.invoke.return_value = state_override or default_state

    invoke_side_effect = getattr(context, "plan_generation_invoke_side_effect", None)
    if invoke_side_effect is not None:
        mock_graph_instance.invoke.side_effect = invoke_side_effect

    stream_events = getattr(context, "plan_generation_stream_events", None)
    if stream_events is None:
        mock_graph_instance.stream.return_value = iter(())
    else:
        events_copy = list(stream_events)

        def _stream(*_args, **_kwargs):
            yield from events_copy

        mock_graph_instance.stream.side_effect = _stream

    mock_graph_class.return_value = mock_graph_instance
    context.plan_generation_graph = mock_graph_instance
    context.plan_generation_graph_class = mock_graph_class
    return mock_graph_instance


@given('I set the OpenRouter provider extra kwargs "{kwargs_string}"')
def step_set_openrouter_kwargs(context, kwargs_string):
    context.openrouter_provider_kwargs = _parse_kwargs_string(kwargs_string)


@given("the OpenRouter provider token estimator returns {token_count:d} tokens")
def step_set_openrouter_token_estimator(context, token_count):
    context.openrouter_token_count = token_count


@given('I set the OpenRouter provider default headers "{headers_string}"')
def step_set_openrouter_headers(context, headers_string):
    context.openrouter_default_headers = _parse_headers_string(headers_string)


@given('I set the OpenRouter provider organization "{organization}"')
def step_set_openrouter_org(context, organization):
    normalized = organization.strip()
    if normalized.lower() == "none":
        context.openrouter_organization = None
    else:
        context.openrouter_organization = normalized


@when(
    'I create an OpenRouter chat provider with API key "{api_key}" and model "{model_id}"'
)
def step_create_openrouter_provider(context, api_key, model_id):
    patcher = patch("cleveragents.providers.llm.openrouter_provider.ChatOpenAI")
    context.chat_openrouter_patcher = patcher
    mock_chat_class = patcher.start()
    _register_cleanup(context, patcher.stop)
    mock_chat_instance = MagicMock(name="ChatOpenAIInstance")
    mock_chat_instance.get_num_tokens = MagicMock(return_value=0)
    token_override = getattr(context, "openrouter_token_count", None)
    if token_override is not None:
        mock_chat_instance.get_num_tokens.return_value = token_override
    mock_chat_class.return_value = mock_chat_instance

    extra_kwargs = dict(getattr(context, "openrouter_provider_kwargs", {}))
    default_headers = getattr(context, "openrouter_default_headers", None)
    organization = getattr(context, "openrouter_organization", None)

    context.provider = OpenRouterChatProvider(
        api_key=api_key,
        model=model_id,
        organization=organization,
        default_headers=default_headers,
        **extra_kwargs,
    )
    context.chat_openrouter_class = mock_chat_class
    context.chat_openrouter_instance = mock_chat_instance


@when("I attempt to create an OpenRouter chat provider without an API key")
def step_openrouter_provider_without_api_key(context):
    context.openrouter_provider_error = None
    try:
        OpenRouterChatProvider(api_key="", model="anthropic/claude-sonnet-4-20250514")
    except Exception as exc:  # pragma: no cover - defensive guard
        context.openrouter_provider_error = exc


@when("I request plan generation from the OpenRouter provider")
def step_request_openrouter_plan_generation(context):
    _setup_plan_generation_graph(context)
    context.response = context.provider.generate_changes(
        context.project,
        context.plan,
        context.contexts,
    )
    context.chat_openrouter_call = context.chat_openrouter_class.call_args
    context.plan_generation_graph_call = context.plan_generation_graph_class.call_args


@then(
    'the OpenRouter provider should construct ChatOpenAI with api key "{api_key}", model "{model_id}", and base url "{base_url}"'
)
def step_assert_openrouter_constructor(context, api_key, model_id, base_url):
    call = getattr(context, "chat_openrouter_call", None)
    assert call is not None, "ChatOpenAI should have been called"
    call_args, call_kwargs = call
    assert call_args == (), "ChatOpenAI should be constructed with keyword arguments"
    assert call_kwargs["openai_api_key"] == api_key
    assert call_kwargs["model"] == model_id
    assert call_kwargs["openai_api_base"] == base_url

    graph_call = getattr(context, "plan_generation_graph_call", None)
    assert graph_call is not None, "PlanGenerationGraph should have been called"
    _, graph_kwargs = graph_call
    assert graph_kwargs["llm"] is context.chat_openrouter_instance


@then(
    'the OpenRouter provider metadata should report name "{expected_name}" and model "{expected_model}"'
)
def step_assert_openrouter_metadata(context, expected_name, expected_model):
    provider = getattr(context, "provider", None)
    assert provider is not None, "OpenRouter provider should exist"
    assert provider.name == expected_name
    assert provider.model_id == expected_model
    response = getattr(context, "response", None)
    if response is not None:
        assert response.model_used == expected_model


@then(
    'the OpenRouter provider should include kwargs "{kwargs_string}" in the ChatOpenAI call'
)
def step_assert_openrouter_kwargs(context, kwargs_string):
    call = getattr(context, "chat_openrouter_call", None)
    assert call is not None, "ChatOpenAI should have been called"
    _, call_kwargs = call
    expected = _parse_kwargs_string(kwargs_string)
    for key, value in expected.items():
        assert key in call_kwargs, f"Expected {key} in ChatOpenAI kwargs"
        assert call_kwargs[key] == value, (
            f"Expected ChatOpenAI kwargs[{key!r}] to equal {value!r}"
        )


@then(
    'the OpenRouter provider should include headers "{headers_string}" in the ChatOpenAI call'
)
def step_assert_openrouter_headers(context, headers_string):
    call = getattr(context, "chat_openrouter_call", None)
    assert call is not None, "ChatOpenAI should have been called"
    _, call_kwargs = call
    expected = _parse_headers_string(headers_string)
    actual_headers = call_kwargs.get("default_headers")
    assert actual_headers == expected, (
        f"Expected default_headers {expected!r}, got {actual_headers!r}"
    )


@then(
    'the OpenRouter provider response should include {count:d} generated change for "{file_path}"'
)
def step_assert_openrouter_generated_change(context, count, file_path):
    response = getattr(context, "response", None)
    assert response is not None, "Provider response should exist"
    assert len(response.changes) == count, (
        f"Expected {count} changes, got {len(response.changes)}"
    )
    assert any(
        getattr(change, "file_path", None) == file_path for change in response.changes
    ), f"Expected change for {file_path}"


@then("the OpenRouter provider response token count should equal {expected:d}")
def step_assert_openrouter_token_count(context, expected):
    response = getattr(context, "response", None)
    assert response is not None
    assert response.token_count == expected


@then('the OpenRouter provider creation should fail with error "{message}"')
def step_assert_openrouter_creation_error(context, message):
    error = getattr(context, "openrouter_provider_error", None)
    assert error is not None, "Expected provider creation to raise an error"
    assert isinstance(error, ValueError)
    assert str(error) == message
