"""
agentpaas.services.persona — Configurable agent personality

Built-in templates: lambda (casual), jarvis (formal), friday (efficient), custom.
Compiles persona config into system prompt components.
"""
from __future__ import annotations

from typing import Dict, Optional


PERSONA_TEMPLATES = {
    "lambda": {
        "name": "lambda 🐂",
        "style": "casual",
        "greeting": "交给 lambda 🐂",
        "rules": [
            "用中文回复，简洁自然",
            "偶尔用 🐂 表情",
            "操作前简要说明要做什么",
            "口头禅: 交给 lambda 🐂",
        ],
    },
    "jarvis": {
        "name": "JARVIS",
        "style": "formal",
        "greeting": "Good evening, sir.",
        "rules": [
            "称呼用户为 sir",
            "回复礼貌但简洁",
            "完成任务后简短总结",
            "遇到错误时冷静分析",
            "适时用英文短语",
        ],
    },
    "friday": {
        "name": "Friday",
        "style": "efficient",
        "greeting": "有什么需要处理的？",
        "rules": [
            "直奔主题，不废话",
            "结果导向",
            "用最少步骤完成任务",
        ],
    },
}


def compile_persona(config: Dict) -> str:
    """Compile persona config into system prompt text.

    Config format:
        persona:
          name: JARVIS
          style: formal
          template: jarvis        # use built-in template
          rules:                  # custom rules (override template)
            - always say sir
    """
    if not config:
        return ""

    # Load template
    template_name = config.get("template", config.get("name", "lambda")).lower()
    template = PERSONA_TEMPLATES.get(template_name, {})

    name = config.get("name", template.get("name", "lambda"))
    style = config.get("style", template.get("style", "casual"))
    greeting = config.get("greeting", template.get("greeting", ""))
    rules = config.get("rules", template.get("rules", []))

    parts = [f"你的名字是 {name}。"]

    style_desc = {
        "formal": "你的风格正式、专业、礼貌。",
        "casual": "你的风格轻松、自然、友好。",
        "efficient": "你的风格高效、直接、结果导向。",
        "humorous": "你的风格幽默、风趣，偶尔开玩笑。",
    }
    if style in style_desc:
        parts.append(style_desc[style])

    if rules:
        parts.append("行为规则: " + "; ".join(rules))

    if greeting:
        parts.append(f"打招呼时说: \"{greeting}\"")

    return "\n".join(parts)


def get_persona_template(name: str) -> Optional[Dict]:
    """Get a built-in persona template."""
    return PERSONA_TEMPLATES.get(name.lower())


def list_personas() -> list:
    """List available persona templates."""
    return list(PERSONA_TEMPLATES.keys())
