"""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.agents import LlmAgent

from adk_fluent._base import BuilderBase

if TYPE_CHECKING:
    from google.adk.agents.base_agent import BaseAgent
    from google.adk.tools.base_tool import BaseTool


class Agent(BuilderBase):
    """An agent powered by a large language model."""

    _ALIASES: dict[str, str] = {"describe": "description", "instruct": "instruction"}
    _CALLBACK_ALIASES: dict[str, str] = {"before_model": "before_model_callback"}
    _ADDITIVE_FIELDS: set[str] = {"before_model_callback"}
    _ADK_TARGET_CLASS = LlmAgent

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

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

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

    def before_model(self, *fns: Callable[..., Any]) -> Self:
        """Append callback(s) to `before_model_callback`. Multiple calls accumulate."""
        self = self._maybe_fork_for_mutation()
        for fn in fns:
            self._callbacks["before_model_callback"].append(fn)
        return self

    def before_model_if(self, condition: bool, fn: Callable[..., Any]) -> Self:
        """Append callback to `before_model_callback` only if condition is True."""
        self = self._maybe_fork_for_mutation()
        if condition:
            self._callbacks["before_model_callback"].append(fn)
        return self

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

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

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

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

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

    def build(self) -> LlmAgent:
        """An agent powered by a large language model. Resolve into a native ADK LlmAgent."""
        config = self._prepare_build_config()
        result = self._safe_build(LlmAgent, config)
        return self._apply_native_hooks(result)
