"""Auto-generated by adk-fluent generator. Manual edits will be overwritten."""

from __future__ import annotations

from collections import defaultdict
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Self

from google.adk.apps.app import App as _ADK_App
from google.adk.runners import InMemoryRunner as _ADK_InMemoryRunner
from google.adk.runners import Runner as _ADK_Runner

from adk_fluent._base import BuilderBase

if TYPE_CHECKING:
    from google.adk.agents.base_agent import BaseAgent
    from google.adk.agents.context_cache_config import ContextCacheConfig
    from google.adk.apps.app import EventsCompactionConfig, ResumabilityConfig
    from google.adk.artifacts.base_artifact_service import BaseArtifactService
    from google.adk.auth.credential_service.base_credential_service import BaseCredentialService
    from google.adk.memory.base_memory_service import BaseMemoryService
    from google.adk.plugins.base_plugin import BasePlugin


class App(BuilderBase):
    """Represents an LLM-backed agentic application."""

    _ALIASES: dict[str, str] = {}
    _CALLBACK_ALIASES: dict[str, str] = {}
    _ADDITIVE_FIELDS: set[str] = set()
    _ADK_TARGET_CLASS = _ADK_App

    def __init__(self, name: str, root_agent: str) -> None:
        self._config: dict[str, Any] = {"name": name, "root_agent": root_agent}
        self._callbacks: dict[str, list[Callable]] = defaultdict(list)
        self._lists: dict[str, list] = defaultdict(list)
        self._frozen = False

    def plugins(self, value: list[BasePlugin]) -> Self:
        """Set the ``plugins`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["plugins"] = value
        return self

    def events_compaction_config(self, value: EventsCompactionConfig | None) -> Self:
        """Set the ``events_compaction_config`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["events_compaction_config"] = value
        return self

    def context_cache_config(self, value: ContextCacheConfig | None) -> Self:
        """Set the ``context_cache_config`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["context_cache_config"] = value
        return self

    def resumability_config(self, value: ResumabilityConfig | None) -> Self:
        """Set the ``resumability_config`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["resumability_config"] = value
        return self

    def plugin(self, value: BasePlugin) -> Self:
        """Append to ``plugins`` (lazy — built at .build() time)."""
        self = self._maybe_fork_for_mutation()
        self._lists["plugins"].append(value)
        return self

    def build(self) -> _ADK_App:
        """Represents an LLM-backed agentic application. Resolve into a native ADK _ADK_App."""
        config = self._prepare_build_config()
        result = self._safe_build(_ADK_App, config)
        return self._apply_native_hooks(result)


class InMemoryRunner(BuilderBase):
    """An in-memory Runner for testing and development."""

    _ALIASES: dict[str, str] = {}
    _CALLBACK_ALIASES: dict[str, str] = {}
    _ADDITIVE_FIELDS: set[str] = set()
    _KNOWN_PARAMS: set[str] | None = {"agent", "app", "app_name", "plugin_close_timeout", "plugins"}

    def __init__(self) -> None:
        self._config: dict[str, Any] = {}
        self._callbacks: dict[str, list[Callable]] = defaultdict(list)
        self._lists: dict[str, list] = defaultdict(list)
        self._frozen = False

    def agent(self, value: BaseAgent | None) -> Self:
        """Set the ``agent`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["agent"] = value
        return self

    def app_name(self, value: str | None) -> Self:
        """Set the ``app_name`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["app_name"] = value
        return self

    def plugins(self, value: list[BasePlugin] | None) -> Self:
        """Set the ``plugins`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["plugins"] = value
        return self

    def app(self, value: App | None) -> Self:
        """Set the ``app`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["app"] = value
        return self

    def plugin_close_timeout(self, value: float) -> Self:
        """Set the ``plugin_close_timeout`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["plugin_close_timeout"] = value
        return self

    def build(self) -> _ADK_InMemoryRunner:
        """An in-memory Runner for testing and development. Resolve into a native ADK _ADK_InMemoryRunner."""
        config = self._prepare_build_config()
        result = self._safe_build(_ADK_InMemoryRunner, config)
        return self._apply_native_hooks(result)


class Runner(BuilderBase):
    """The Runner class is used to run agents."""

    _ALIASES: dict[str, str] = {}
    _CALLBACK_ALIASES: dict[str, str] = {}
    _ADDITIVE_FIELDS: set[str] = set()
    _KNOWN_PARAMS: set[str] | None = {
        "agent",
        "app",
        "app_name",
        "artifact_service",
        "auto_create_session",
        "credential_service",
        "memory_service",
        "plugin_close_timeout",
        "plugins",
        "session_service",
    }

    def __init__(self, session_service: str) -> None:
        self._config: dict[str, Any] = {"session_service": session_service}
        self._callbacks: dict[str, list[Callable]] = defaultdict(list)
        self._lists: dict[str, list] = defaultdict(list)
        self._frozen = False

    def app(self, value: App | None) -> Self:
        """Set the ``app`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["app"] = value
        return self

    def app_name(self, value: str | None) -> Self:
        """Set the ``app_name`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["app_name"] = value
        return self

    def agent(self, value: BaseAgent | None) -> Self:
        """Set the ``agent`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["agent"] = value
        return self

    def plugins(self, value: list[BasePlugin] | None) -> Self:
        """Set the ``plugins`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["plugins"] = value
        return self

    def artifact_service(self, value: BaseArtifactService | None) -> Self:
        """Set the ``artifact_service`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["artifact_service"] = value
        return self

    def memory_service(self, value: BaseMemoryService | None) -> Self:
        """Set the ``memory_service`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["memory_service"] = value
        return self

    def credential_service(self, value: BaseCredentialService | None) -> Self:
        """Set the ``credential_service`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["credential_service"] = value
        return self

    def plugin_close_timeout(self, value: float) -> Self:
        """Set the ``plugin_close_timeout`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["plugin_close_timeout"] = value
        return self

    def auto_create_session(self, value: bool) -> Self:
        """Set the ``auto_create_session`` field."""
        self = self._maybe_fork_for_mutation()
        self._config["auto_create_session"] = value
        return self

    def build(self) -> _ADK_Runner:
        """The Runner class is used to run agents. Resolve into a native ADK _ADK_Runner."""
        config = self._prepare_build_config()
        result = self._safe_build(_ADK_Runner, config)
        return self._apply_native_hooks(result)
