"""ASV benchmarks for server client stubs and connection config.

Measures the performance of:
- ServerConnectionConfig construction and validation
- StubServerClient/StubRemoteExecutionClient/StubAuthClient instantiation
- Config key registry lookups for server.* keys
"""

from __future__ import annotations

import importlib
import sys
from pathlib import Path

# Ensure the local *source* tree is importable even when ASV has an
# older build of the package installed.
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

# Force-reload so ASV picks up the source tree version.
import cleveragents  # noqa: E402

importlib.reload(cleveragents)

from cleveragents.a2a.clients import (  # noqa: E402
    StubAuthClient,
    StubRemoteExecutionClient,
    StubServerClient,
)
from cleveragents.a2a.server_config import ServerConnectionConfig  # noqa: E402
from cleveragents.application.services.config_service import _REGISTRY  # noqa: E402

# ---------------------------------------------------------------------------
# Config resolution benchmarks
# ---------------------------------------------------------------------------


class ServerConfigResolutionSuite:
    """Benchmark ServerConnectionConfig construction overhead."""

    timeout = 60

    def time_create_config_minimal(self) -> None:
        ServerConnectionConfig(server_url="https://agents.example.com")

    def time_create_config_full(self) -> None:
        ServerConnectionConfig(
            server_url="https://agents.example.com",
            namespace="my-team",
            auth_token_ref="CLEVERAGENTS_SERVER_TOKEN",
            tls_verify=False,
        )

    def time_registry_lookup_server_url(self) -> None:
        _ = _REGISTRY["server.url"]

    def time_registry_lookup_server_tls(self) -> None:
        _ = _REGISTRY["server.tls-verify"]

    def time_registry_lookup_server_namespace(self) -> None:
        _ = _REGISTRY["server.namespace"]


# ---------------------------------------------------------------------------
# Stub client benchmarks
# ---------------------------------------------------------------------------


class StubClientInstantiationSuite:
    """Benchmark stub client instantiation overhead."""

    timeout = 60

    def time_instantiate_stub_server_client(self) -> None:
        StubServerClient()

    def time_instantiate_stub_execution_client(self) -> None:
        StubRemoteExecutionClient()

    def time_instantiate_stub_auth_client(self) -> None:
        StubAuthClient()

    def time_instantiate_all_stubs(self) -> None:
        StubServerClient()
        StubRemoteExecutionClient()
        StubAuthClient()
