"""Expert 配置与模板 - ExpertConfig, ExpertTemplate"""

from __future__ import annotations

from dataclasses import dataclass

from agentkit.core.config_driven import AgentConfig


class ExpertConfig(AgentConfig):
    """扩展 AgentConfig，新增 Expert 专属字段

    Expert 是比 Skill 更高层的角色抽象，一个 Expert 聚合多个 Skill，
    并包含 persona、thinking_style、collaboration_strategy 等角色属性。
    """

    def __init__(
        self,
        name: str,
        agent_type: str,
        version: str = "1.0.0",
        description: str = "",
        task_mode: str = "llm_generate",
        supported_tasks: list[str] | None = None,
        max_concurrency: int = 1,
        input_schema: dict[str, object] | None = None,
        output_schema: dict[str, object] | None = None,
        prompt: dict[str, str] | None = None,
        llm: dict[str, object] | None = None,
        tools: list[str] | None = None,
        memory: dict[str, object] | None = None,
        custom_handler: str | None = None,
        # Expert 专属字段
        persona: str = "",
        thinking_style: str = "",
        collaboration_strategy: str = "cooperative",
        bound_skills: list[str] | None = None,
        avatar: str = "",
        color: str = "#1890ff",
        is_lead: bool = False,
        # Board Meeting 模式字段
        speaking_style: str = "",
        decision_framework: str = "",
    ):
        super().__init__(
            name=name,
            agent_type=agent_type,
            version=version,
            description=description,
            task_mode=task_mode,
            supported_tasks=supported_tasks,
            max_concurrency=max_concurrency,
            input_schema=input_schema,
            output_schema=output_schema,
            prompt=prompt,
            llm=llm,
            tools=tools,
            memory=memory,
            custom_handler=custom_handler,
        )
        self.persona = persona
        self.thinking_style = thinking_style
        self.collaboration_strategy = collaboration_strategy
        self.bound_skills = bound_skills or []
        self.avatar = avatar
        self.color = color
        self.is_lead = is_lead
        self.speaking_style = speaking_style
        self.decision_framework = decision_framework

    @classmethod
    def from_dict(cls, data: dict[str, object]) -> ExpertConfig:
        """从字典创建配置"""
        return cls(
            name=data["name"],
            agent_type=data["agent_type"],
            version=data.get("version", "1.0.0"),
            description=data.get("description", ""),
            task_mode=data.get("task_mode", "llm_generate"),
            supported_tasks=data.get("supported_tasks"),
            max_concurrency=data.get("max_concurrency", 1),
            input_schema=data.get("input_schema"),
            output_schema=data.get("output_schema"),
            prompt=data.get("prompt"),
            llm=data.get("llm"),
            tools=data.get("tools"),
            memory=data.get("memory"),
            custom_handler=data.get("custom_handler"),
            persona=data.get("persona", ""),
            thinking_style=data.get("thinking_style", ""),
            collaboration_strategy=data.get("collaboration_strategy", "cooperative"),
            bound_skills=data.get("bound_skills"),
            avatar=data.get("avatar", ""),
            color=data.get("color", "#1890ff"),
            is_lead=data.get("is_lead", False),
            speaking_style=data.get("speaking_style", ""),
            decision_framework=data.get("decision_framework", ""),
        )

    def to_dict(self) -> dict[str, object]:
        """序列化为字典，包含 Expert 专属字段"""
        d = super().to_dict()
        d["persona"] = self.persona
        d["thinking_style"] = self.thinking_style
        d["collaboration_strategy"] = self.collaboration_strategy
        d["bound_skills"] = self.bound_skills
        d["avatar"] = self.avatar
        d["color"] = self.color
        d["is_lead"] = self.is_lead
        d["speaking_style"] = self.speaking_style
        d["decision_framework"] = self.decision_framework
        return d


@dataclass
class ExpertTemplate:
    """Expert 模板 - 可复用的 Expert 配置模板

    用于预定义 Expert 角色配置，支持内置模板和用户自定义模板。
    """

    name: str
    config: ExpertConfig
    is_builtin: bool = False
    description: str = ""

    def to_dict(self) -> dict[str, object]:
        """序列化为字典"""
        return {
            "name": self.name,
            "config": self.config.to_dict(),
            "is_builtin": self.is_builtin,
            "description": self.description,
        }

    @classmethod
    def from_dict(cls, data: dict[str, object]) -> ExpertTemplate:
        """从字典创建模板"""
        config_data = data["config"]
        config = ExpertConfig.from_dict(config_data)
        return cls(
            name=data["name"],
            config=config,
            is_builtin=data.get("is_builtin", False),
            description=data.get("description", ""),
        )
