"""Tests for OpenAIAgentsKivotPlugin."""

import pytest
from datetime import timedelta

from temporalio.common import RetryPolicy

from .overrides import (
    ActivityParameters,
    KivotAgentsConfig,
    get_kivot_config,
    set_kivot_agent_overrides,
)
from .plugin import OpenAIAgentsKivotPlugin


def test_plugin_initialization():
    """Test plugin initializes with defaults."""
    plugin = OpenAIAgentsKivotPlugin()

    assert plugin.config.go_activities_task_queue == "core-activities"
    assert plugin.config.model_params.start_to_close_timeout == timedelta(seconds=60)
    assert plugin.config.tool_params.start_to_close_timeout == timedelta(seconds=30)
    assert plugin.config.model_params.retry_policy.maximum_attempts == 3
    assert plugin.config.tool_params.retry_policy.maximum_attempts == 2


def test_plugin_custom_params():
    """Test plugin with custom parameters."""
    plugin = OpenAIAgentsKivotPlugin(
        model_params=ActivityParameters(start_to_close_timeout=timedelta(seconds=120)),
        go_activities_task_queue="custom-queue",
        default_policy_refs=["policy1", "policy2"],
    )

    assert plugin.config.go_activities_task_queue == "custom-queue"
    assert plugin.config.model_params.start_to_close_timeout == timedelta(seconds=120)
    assert plugin.config.default_policy_refs == ["policy1", "policy2"]


def test_plugin_custom_retry_policy():
    """Test plugin with custom retry policies."""
    custom_retry = RetryPolicy(
        maximum_attempts=5, initial_interval=timedelta(seconds=3)
    )

    plugin = OpenAIAgentsKivotPlugin(
        model_params=ActivityParameters(retry_policy=custom_retry)
    )

    assert plugin.config.model_params.retry_policy.maximum_attempts == 5
    assert plugin.config.model_params.retry_policy.initial_interval == timedelta(
        seconds=3
    )


def test_overrides_context_manager():
    """Test set_kivot_agent_overrides context manager."""
    config = KivotAgentsConfig(
        model_params=ActivityParameters(start_to_close_timeout=timedelta(seconds=30)),
        tool_params=ActivityParameters(start_to_close_timeout=timedelta(seconds=15)),
    )

    # Outside context
    assert get_kivot_config() is None

    # Inside context
    with set_kivot_agent_overrides(config):
        current = get_kivot_config()
        assert current is not None
        assert current.model_params.start_to_close_timeout == timedelta(seconds=30)
        assert current.tool_params.start_to_close_timeout == timedelta(seconds=15)

    # After context
    assert get_kivot_config() is None


def test_nested_overrides():
    """Test nested override contexts."""
    config1 = KivotAgentsConfig(
        model_params=ActivityParameters(start_to_close_timeout=timedelta(seconds=30)),
        tool_params=ActivityParameters(start_to_close_timeout=timedelta(seconds=15)),
    )

    config2 = KivotAgentsConfig(
        model_params=ActivityParameters(start_to_close_timeout=timedelta(seconds=60)),
        tool_params=ActivityParameters(start_to_close_timeout=timedelta(seconds=30)),
    )

    with set_kivot_agent_overrides(config1):
        assert get_kivot_config().model_params.start_to_close_timeout == timedelta(
            seconds=30
        )

        with set_kivot_agent_overrides(config2):
            assert (
                get_kivot_config().model_params.start_to_close_timeout
                == timedelta(seconds=60)
            )

        # Restored to config1
        assert get_kivot_config().model_params.start_to_close_timeout == timedelta(
            seconds=30
        )

    # After all contexts
    assert get_kivot_config() is None


def test_config_task_queue():
    """Test task queue configuration."""
    config = KivotAgentsConfig(
        model_params=ActivityParameters(),
        tool_params=ActivityParameters(),
        go_activities_task_queue="my-custom-queue",
    )

    with set_kivot_agent_overrides(config):
        current = get_kivot_config()
        assert current.go_activities_task_queue == "my-custom-queue"


def test_config_default_policy_refs():
    """Test default policy refs configuration."""
    config = KivotAgentsConfig(
        model_params=ActivityParameters(),
        tool_params=ActivityParameters(),
        default_policy_refs=["policy-a", "policy-b", "policy-c"],
    )

    with set_kivot_agent_overrides(config):
        current = get_kivot_config()
        assert current.default_policy_refs == ["policy-a", "policy-b", "policy-c"]
