"""Adaptive runtime learning system."""

from __future__ import annotations

from collections import defaultdict


LEARNING_STATE: dict[str, dict] = defaultdict(dict)


def track_response_feedback(style: str, positive: bool) -> None:
    state = LEARNING_STATE.setdefault(
        style,
        {
            "positive": 0,
            "negative": 0,
        },
    )

    if positive:
        state["positive"] += 1
    else:
        state["negative"] += 1


def preferred_style() -> str:
    if not LEARNING_STATE:
        return "balanced"

    scored = {}

    for style, state in LEARNING_STATE.items():
        scored[style] = state["positive"] - state["negative"]

    return max(scored.items(), key=lambda item: item[1])[0]


def learning_instruction() -> str:
    style = preferred_style()

    instructions = {
        "funny": "کاربرا بیشتر جواب‌های شوخ و playful رو دوست دارن.",
        "serious": "کاربرا جواب‌های دقیق و جدی رو ترجیح میدن.",
        "tech": "کاربرا vibe فنی و nerdy رو دوست دارن.",
        "chill": "کاربرا vibe آروم و دوستانه رو دوست دارن.",
        "balanced": "بین شوخی، صمیمیت و دقت تعادل نگه دار.",
    }

    return instructions.get(style, instructions["balanced"])
