"""ExpertConfig 和 ExpertTemplate 单元测试"""

from __future__ import annotations

from typing import Any

import pytest

from agentkit.core.config_driven import AgentConfig
from agentkit.experts.config import ExpertConfig, ExpertTemplate


# ── 辅助函数 ──────────────────────────────────────────────


def _make_expert_config(
    name: str = "test_expert",
    agent_type: str = "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,
    **kwargs: Any,
) -> ExpertConfig:
    """创建测试用 ExpertConfig 实例"""
    return ExpertConfig(
        name=name,
        agent_type=agent_type,
        persona=persona,
        thinking_style=thinking_style,
        collaboration_strategy=collaboration_strategy,
        bound_skills=bound_skills,
        avatar=avatar,
        color=color,
        is_lead=is_lead,
        **kwargs,
    )


# ── ExpertConfig 测试 ─────────────────────────────────────


class TestExpertConfig:
    """ExpertConfig 配置测试"""

    def test_creation_with_all_fields(self):
        """创建 ExpertConfig 并设置所有字段"""
        config = ExpertConfig(
            name="analyst",
            agent_type="analyst",
            persona="善于数据分析的专家",
            thinking_style="逻辑推理",
            collaboration_strategy="cooperative",
            bound_skills=["data_query", "chart_gen"],
            avatar="📊",
            color="#52c41a",
            is_lead=True,
            task_mode="llm_generate",
            prompt={"identity": "数据分析师"},
        )
        assert config.name == "analyst"
        assert config.agent_type == "analyst"
        assert config.persona == "善于数据分析的专家"
        assert config.thinking_style == "逻辑推理"
        assert config.collaboration_strategy == "cooperative"
        assert config.bound_skills == ["data_query", "chart_gen"]
        assert config.avatar == "📊"
        assert config.color == "#52c41a"
        assert config.is_lead is True

    def test_inherits_agent_config_fields(self):
        """ExpertConfig 继承 AgentConfig 基础字段"""
        config = ExpertConfig(
            name="test",
            agent_type="test",
            version="2.0.0",
            description="测试专家",
            task_mode="llm_generate",
            prompt={"identity": "测试"},
            llm={"model": "gpt-4"},
            tools=["search"],
        )
        assert isinstance(config, AgentConfig)
        assert config.version == "2.0.0"
        assert config.description == "测试专家"
        assert config.prompt == {"identity": "测试"}
        assert config.llm == {"model": "gpt-4"}
        assert config.tools == ["search"]

    def test_to_dict_from_dict_roundtrip(self):
        """to_dict / from_dict 往返序列化"""
        config = ExpertConfig(
            name="roundtrip",
            agent_type="expert",
            persona="测试角色",
            thinking_style="创造性思维",
            collaboration_strategy="lead",
            bound_skills=["skill_a", "skill_b"],
            avatar="🧠",
            color="#ff4d4f",
            is_lead=True,
            task_mode="llm_generate",
            prompt={"identity": "往返测试"},
        )
        d = config.to_dict()
        restored = ExpertConfig.from_dict(d)

        assert restored.name == config.name
        assert restored.agent_type == config.agent_type
        assert restored.persona == config.persona
        assert restored.thinking_style == config.thinking_style
        assert restored.collaboration_strategy == config.collaboration_strategy
        assert restored.bound_skills == config.bound_skills
        assert restored.avatar == config.avatar
        assert restored.color == config.color
        assert restored.is_lead == config.is_lead
        assert restored.prompt == config.prompt

    def test_bound_skills_defaults_to_empty_list(self):
        """bound_skills 未设置时默认为空列表"""
        config = ExpertConfig(
            name="no_skills",
            agent_type="expert",
            task_mode="llm_generate",
            prompt={"identity": "无技能"},
        )
        assert config.bound_skills == []

    def test_default_values(self):
        """ExpertConfig 默认值测试"""
        config = ExpertConfig(
            name="defaults",
            agent_type="expert",
            task_mode="llm_generate",
            prompt={"identity": "默认"},
        )
        assert config.persona == ""
        assert config.thinking_style == ""
        assert config.collaboration_strategy == "cooperative"
        assert config.bound_skills == []
        assert config.avatar == ""
        assert config.color == "#1890ff"
        assert config.is_lead is False


# ── ExpertTemplate 测试 ───────────────────────────────────


class TestExpertTemplate:
    """ExpertTemplate 模板测试"""

    def test_creation(self):
        """创建 ExpertTemplate"""
        config = ExpertConfig(
            name="analyst",
            agent_type="analyst",
            persona="数据分析师",
            task_mode="llm_generate",
            prompt={"identity": "分析师"},
        )
        template = ExpertTemplate(
            name="analyst_template",
            config=config,
            is_builtin=True,
            description="内置数据分析师模板",
        )
        assert template.name == "analyst_template"
        assert template.config is config
        assert template.is_builtin is True
        assert template.description == "内置数据分析师模板"

    def test_creation_defaults(self):
        """ExpertTemplate 默认值"""
        config = ExpertConfig(
            name="default_expert",
            agent_type="expert",
            task_mode="llm_generate",
            prompt={"identity": "默认"},
        )
        template = ExpertTemplate(name="default_template", config=config)
        assert template.is_builtin is False
        assert template.description == ""

    def test_to_dict_from_dict_roundtrip(self):
        """ExpertTemplate to_dict / from_dict 往返序列化"""
        config = ExpertConfig(
            name="roundtrip_expert",
            agent_type="expert",
            persona="往返测试",
            thinking_style="分析型",
            collaboration_strategy="cooperative",
            bound_skills=["skill_x"],
            avatar="🔬",
            color="#722ed1",
            is_lead=False,
            task_mode="llm_generate",
            prompt={"identity": "往返"},
        )
        template = ExpertTemplate(
            name="roundtrip_template",
            config=config,
            is_builtin=False,
            description="往返测试模板",
        )
        d = template.to_dict()
        restored = ExpertTemplate.from_dict(d)

        assert restored.name == template.name
        assert restored.is_builtin == template.is_builtin
        assert restored.description == template.description
        assert restored.config.name == config.name
        assert restored.config.persona == config.persona
        assert restored.config.thinking_style == config.thinking_style
        assert restored.config.collaboration_strategy == config.collaboration_strategy
        assert restored.config.bound_skills == config.bound_skills
        assert restored.config.avatar == config.avatar
        assert restored.config.color == config.color
        assert restored.config.is_lead == config.is_lead
