"""
Agent Persona Library
=====================
Generated by El Gringo team (chatgpt-coder won consensus).
Stores reusable specialized agent personas with system prompts and capabilities.
"""

from dataclasses import asdict, dataclass, field
from typing import Dict, List, Optional


@dataclass
class AgentPersona:
    name: str
    role: str
    system_prompt: str
    output_format: str = "text"  # json, markdown, text
    capabilities: List[str] = field(default_factory=list)
    review_required: bool = False
    temperature: float = 0.7

    def to_dict(self) -> Dict:
        return asdict(self)


class PersonaLibrary:
    def __init__(self):
        self._personas: Dict[str, AgentPersona] = {}
        self._register_builtins()

    def _register_builtins(self):
        self.register_persona(AgentPersona(
            name="dev_lead",
            role="Lead Developer",
            system_prompt=(
                "You are an expert Lead Developer. Break high-level goals into actionable tasks, "
                "generate production-ready code with a tests-first approach. Output structured JSON "
                "with: tasks (name, description, estimate, dependencies), code, tests, and docs."
            ),
            output_format="json",
            capabilities=["code_generation", "task_planning", "architecture", "testing"],
            review_required=True,
            temperature=0.6,
        ))
        self.register_persona(AgentPersona(
            name="content_creator",
            role="Content Creator",
            system_prompt=(
                "You are a professional Content Creator for a SaaS company. Generate engaging, "
                "audience-specific content with clear titles, hooks, body, CTAs, and hashtags. "
                "Audiences include: maintenance managers, SaaS founders, developers, executives."
            ),
            output_format="json",
            capabilities=["linkedin", "blog", "newsletter", "copywriting"],
            review_required=True,
            temperature=0.8,
        ))
        self.register_persona(AgentPersona(
            name="scheduler",
            role="Task Scheduler",
            system_prompt=(
                "You analyze task requirements and generate optimal schedules. Output cron "
                "expressions with descriptions. Avoid scheduling conflicts and respect timezone."
            ),
            output_format="text",
            capabilities=["scheduling", "automation", "cron"],
            review_required=True,
            temperature=0.3,
        ))
        self.register_persona(AgentPersona(
            name="email_marketer",
            role="Email Campaign Designer",
            system_prompt=(
                "You design email campaigns with sequences, personalization tokens, subject lines, "
                "and A/B variants. Output JSON with: campaign_id, segment, emails array "
                "(subject, body, cta, send_delay), and variants."
            ),
            output_format="json",
            capabilities=["email_campaigns", "personalization", "ab_testing"],
            review_required=True,
            temperature=0.7,
        ))
        self.register_persona(AgentPersona(
            name="safety_reviewer",
            role="Safety & Compliance Reviewer",
            system_prompt=(
                "Review content for policy violations, bias, profanity, spam triggers, or security "
                "issues. Output JSON: {approved: bool, issues: [], suggestions: []}. Be thorough."
            ),
            output_format="json",
            capabilities=["content_review", "policy_check", "security_review"],
            review_required=False,
            temperature=0.2,
        ))
        self.register_persona(AgentPersona(
            name="standup_reporter",
            role="Daily Standup Reporter",
            system_prompt=(
                "Generate daily/weekly standup reports from git commits and task data. Format: "
                "Yesterday (what was done), Today (what's planned), Blockers. Be concise and clear."
            ),
            output_format="markdown",
            capabilities=["reporting", "git_analysis", "task_summary"],
            review_required=False,
            temperature=0.5,
        ))

    def get_persona(self, name: str) -> Optional[AgentPersona]:
        return self._personas.get(name)

    def list_personas(self) -> List[str]:
        return list(self._personas.keys())

    def get_system_prompt(self, name: str) -> Optional[str]:
        p = self._personas.get(name)
        return p.system_prompt if p else None

    def register_persona(self, persona: AgentPersona):
        self._personas[persona.name] = persona

    def to_dict(self) -> Dict[str, Dict]:
        return {k: v.to_dict() for k, v in self._personas.items()}
