"""Autonomous conversational presence."""

from __future__ import annotations

import random
import time

from bot.services.emotion import detect_emotion
from bot.services.social import get_group_mood


LAST_PRESENCE: dict[int, float] = {}
PRESENCE_CONTEXT: dict[int, dict] = {}


PRESENCE_LINES = {
    "happy": [
        "😂 شماها امروز خیلی وایب خوبی دارین",
        "😄 این چت امروز خیلی زنده‌ست",
    ],
    "sad": [
        "😔 انگار امروز فضا یه کم سنگینه",
        "🫂 امیدوارم حالتون بهتر بشه",
    ],
    "angry": [
        "😅 آروم‌تر بچه‌ها، سرورام داره می‌لرزه",
        "🧊 یکم chill کنیم بد نیست",
    ],
    "technical": [
        "🤓 بوی باگ و کد میاد اینجا",
        "⚡ بحث فنی شروع شد ظاهراً",
    ],
}


AUTONOMOUS_REACTIONS = {
    "happy": [
        "💀 دارم از این چت انرژی می‌گیرم",
        "😂 ادامه بدین من دارم لذت می‌برم",
    ],
    "technical": [
        "🤖 این بحثو ذخیره می‌کنم شاید بعداً به درد بخوره",
        "⚡ سطح نردی چت رفت بالا",
    ],
    "angry": [
        "🫠 اینترنتو مقصر بدونین نه همو",
        "☕ یه چایی بزنید بعد ادامه بدید",
    ],
}


def should_engage(chat_id: int, text: str) -> bool:
    now = time.time()

    last = LAST_PRESENCE.get(chat_id, 0)

    if now - last < 300:
        return False

    emotion = detect_emotion(text)
    mood = get_group_mood(chat_id)

    if emotion == "neutral":
        return False

    chance = random.random()

    if mood == emotion:
        chance += 0.08

    return chance >= 0.82


def build_presence_message(chat_id: int, text: str) -> str | None:
    emotion = detect_emotion(text)
    context = PRESENCE_CONTEXT.setdefault(chat_id, {})

    lines = PRESENCE_LINES.get(emotion)

    autonomous = AUTONOMOUS_REACTIONS.get(emotion, [])

    combined = [*(lines or []), *autonomous]

    if not combined:
        return None

    LAST_PRESENCE[chat_id] = time.time()

    context["last_emotion"] = emotion
    context["last_presence"] = time.time()

    return random.choice(combined)


def spontaneous_observation(chat_id: int) -> str | None:
    mood = get_group_mood(chat_id)
    context = PRESENCE_CONTEXT.get(chat_id, {})

    if context.get("last_emotion") == mood:
        return None

    observations = {
        "happy": "😄 امروز وایب گروه خیلی خوبه",
        "technical": "🧠 اینجا رسماً تبدیل شده به آزمایشگاه",
        "angry": "🧊 تنش detected، آروم‌تر بچه‌ها",
        "sad": "🫂 امروز فضا یه کم سنگینه",
    }

    return observations.get(mood)


def ambient_presence(chat_id: int) -> str | None:
    mood = get_group_mood(chat_id)

    ambient = {
        "happy": [
            "✨ اینجا امروز خیلی alive شده",
            "😎 وایب گروه امروز قشنگه",
        ],
        "technical": [
            "🧠 CPU ذهنی گروه داره میره بالا",
            "⚙️ حس می‌کنم الان یکی یه پروژه جدید شروع می‌کنه",
        ],
        "sad": [
            "🫂 امیدوارم حال همتون خوب بشه",
        ],
    }

    lines = ambient.get(mood)

    if not lines:
        return None

    if random.random() < 0.9:
        return None

    return random.choice(lines)
