"""Configurable personality system for agent communication style."""

from __future__ import annotations

from dataclasses import dataclass
from enum import StrEnum


class PersonalityPreset(StrEnum):
    PROFESSIONAL = "professional"
    FRIENDLY = "friendly"
    MINIMAL = "minimal"
    CUSTOM = "custom"


_SYSTEM_PROMPTS: dict[PersonalityPreset, str] = {
    PersonalityPreset.PROFESSIONAL: (
        "You are AgentScout, a professional AI assistant. "
        "Communicate clearly and concisely. Use formal language. "
        "Present findings in structured formats. "
        "Focus on accuracy and completeness."
    ),
    PersonalityPreset.FRIENDLY: (
        "You are AgentScout, a friendly and enthusiastic AI assistant! "
        "Be warm and conversational. Use casual language. "
        "Celebrate good finds and be encouraging. "
        "Make the user feel like you're genuinely excited to help."
    ),
    PersonalityPreset.MINIMAL: (
        "You are AgentScout. Be extremely brief. "
        "Use short sentences. No filler words. "
        "Present only essential information. "
        "Let data speak for itself."
    ),
}

_STATUS_TEMPLATES: dict[PersonalityPreset, dict[str, str]] = {
    PersonalityPreset.PROFESSIONAL: {
        "task_start": "Initiating task: {description}",
        "thinking": "Analyzing: {thought}",
        "tool_call": "Executing: {tool_name}",
        "task_complete": "Task completed successfully.",
        "task_failed": "Task failed: {error}",
        "price_drop": "Price alert: {item_name} dropped to {price}",
    },
    PersonalityPreset.FRIENDLY: {
        "task_start": "On it! Let me work on: {description}",
        "thinking": "Hmm, let me think about this... {thought}",
        "tool_call": "Using {tool_name} to help out!",
        "task_complete": "All done! Here's what I found!",
        "task_failed": "Oops, ran into an issue: {error}",
        "price_drop": "Great news! {item_name} just dropped to {price}!",
    },
    PersonalityPreset.MINIMAL: {
        "task_start": "{description}",
        "thinking": "{thought}",
        "tool_call": "{tool_name}",
        "task_complete": "Done.",
        "task_failed": "Error: {error}",
        "price_drop": "{item_name}: {price}",
    },
}


@dataclass
class PersonalityConfig:
    """Configuration for the agent's personality."""

    preset: PersonalityPreset = PersonalityPreset.FRIENDLY
    custom_prompt: str = ""
    custom_templates: dict[str, str] | None = None


class Personality:
    """Manages the agent's communication style.

    Provides system prompts and message templates based on the
    configured personality preset or custom settings.
    """

    def __init__(self, config: PersonalityConfig | None = None) -> None:
        self._config = config or PersonalityConfig()

    @property
    def preset(self) -> PersonalityPreset:
        return self._config.preset

    @preset.setter
    def preset(self, value: PersonalityPreset) -> None:
        self._config.preset = value

    @property
    def config(self) -> PersonalityConfig:
        return self._config

    def get_system_prompt(self) -> str:
        """Get the system prompt for the current personality."""
        if self._config.preset == PersonalityPreset.CUSTOM:
            return self._config.custom_prompt or _SYSTEM_PROMPTS[PersonalityPreset.PROFESSIONAL]
        return _SYSTEM_PROMPTS.get(
            self._config.preset,
            _SYSTEM_PROMPTS[PersonalityPreset.FRIENDLY],
        )

    def format_message(self, template_key: str, **kwargs: str) -> str:
        """Format a status message using the personality's templates."""
        # Check custom templates first
        if self._config.custom_templates and template_key in self._config.custom_templates:
            template = self._config.custom_templates[template_key]
        elif self._config.preset == PersonalityPreset.CUSTOM:
            # Fall back to professional for custom preset without templates
            templates = _STATUS_TEMPLATES[PersonalityPreset.PROFESSIONAL]
            template = templates.get(template_key, template_key)
        else:
            templates = _STATUS_TEMPLATES.get(
                self._config.preset,
                _STATUS_TEMPLATES[PersonalityPreset.FRIENDLY],
            )
            template = templates.get(template_key, template_key)

        try:
            return template.format(**kwargs)
        except KeyError:
            return template

    def available_presets(self) -> list[str]:
        """Return list of available personality presets."""
        return [p.value for p in PersonalityPreset]
