"""Mood and emotion detection engine."""

from __future__ import annotations


EMOTION_PATTERNS = {
    "happy": ("😂", "🤣", "خوشحال", "باحال", "عالی", "خفن"),
    "sad": ("غم", "ناراحت", "خسته", "داغون", "😔", "😢"),
    "angry": ("حرص", "اعصاب", "خفه", "لعنت", "😡"),
    "romantic": ("عشق", "دوست دارم", "❤️", "😍"),
    "technical": ("کد", "پایتون", "ربات", "سرور", "برنامه"),
}


def detect_emotion(text: str) -> str:
    lowered = text.lower()

    scores: dict[str, int] = {}

    for emotion, patterns in EMOTION_PATTERNS.items():
        score = 0

        for pattern in patterns:
            if pattern in lowered:
                score += 1

        scores[emotion] = score

    best = max(scores.items(), key=lambda item: item[1])

    return best[0] if best[1] > 0 else "neutral"


def build_emotion_instruction(emotion: str) -> str:
    instructions = {
        "happy": "با انرژی، شوخ و صمیمی جواب بده.",
        "sad": "آروم، همدل و حمایتگر جواب بده.",
        "angry": "آروم، کنترل‌شده و بدون تحریک جواب بده.",
        "romantic": "گرم، دوستانه و نرم جواب بده.",
        "technical": "فنی، دقیق ولی دوستانه جواب بده.",
        "neutral": "طبیعی و دوستانه جواب بده.",
    }

    return instructions.get(emotion, instructions["neutral"])
