"""Persona Store — manages user identity, preferences, and communication style."""

from __future__ import annotations

from typing import Dict, Optional

from .store import MemoryStore

# Default persona keys and their descriptions
_PERSONA_KEYS = {
    "name": "User's preferred name",
    "role": "User's job title or role",
    "communication_style": "Preferred communication style (e.g. concise, detailed, casual)",
    "expertise": "Areas of expertise",
    "preferences": "General preferences and working style",
    "context": "Additional context about the user",
}


def set_persona(store: MemoryStore, key: str, value: str) -> None:
    """Set a persona attribute."""
    store.set_persona(key, value)


def get_persona(store: MemoryStore, key: str) -> Optional[str]:
    """Get a single persona attribute."""
    return store.get_persona(key)


def get_all_persona(store: MemoryStore) -> Dict[str, str]:
    """Get all persona attributes as a dict."""
    return store.get_all_persona()


def update_persona(store: MemoryStore, updates: Dict[str, str]) -> None:
    """Batch-update persona attributes."""
    for key, value in updates.items():
        store.set_persona(key, value)


def clear_persona(store: MemoryStore) -> None:
    """Remove all persona data."""
    store.delete_persona()


def format_persona_prompt(store: MemoryStore) -> str:
    """Format persona data into a system prompt prefix.

    Returns an empty string if no persona is set.
    """
    data = store.get_all_persona()
    if not data:
        return ""

    lines = ["## User Profile"]
    for key, value in sorted(data.items()):
        label = key.replace("_", " ").title()
        lines.append(f"- **{label}**: {value}")
    return "\n".join(lines)
