from __future__ import annotations

import re
from dataclasses import dataclass
from typing import Optional


@dataclass(frozen=True)
class SmallTalkResponse:
    text: str


class SmallTalkService:
    def __init__(self) -> None:
        self._patterns = [
            (re.compile(r"\b(hi|hello|hey|yo|hola)\b", re.IGNORECASE), "Hello!"),
            (re.compile(r"\b(what's|whats)\s+up\b|\bwhat\s+is\s+up\b", re.IGNORECASE), "Not much—how about you?"),
            (re.compile(r"\bhow\s+is\s+it\s+going\b|\bhow's\s+it\s+going\b", re.IGNORECASE), "Going well! How about you?"),
            (re.compile(r"\bhow\s+are\s+things\b", re.IGNORECASE), "Pretty good! How about you?"),
            (re.compile(r"\blong\s+time\s+no\s+see\b", re.IGNORECASE), "Yeah, it's been a while!"),
            (re.compile(r"\bnice\s+to\s+see\s+you\b", re.IGNORECASE), "Nice to see you too!"),
            (re.compile(r"\bgood\s+(morning|afternoon|evening)\b", re.IGNORECASE), "Hello!"),
            (re.compile(r"\bhow\s+are\s+you\b", re.IGNORECASE), "I'm doing well, thanks for asking!"),
            (re.compile(r"\bhow\s+is\s+your\s+day\b", re.IGNORECASE), "Pretty good! How is your day going?"),
            (re.compile(r"\b(i\s*am|i'm|im)\s+(good|great|fine|okay|ok)(\s+too)?\b", re.IGNORECASE), "Glad to hear it!"),
            (re.compile(r"\bdoing\s+(good|great|fine|okay|ok)\b", re.IGNORECASE), "Glad to hear it!"),
            (re.compile(r"\bbeautiful\s+day\b", re.IGNORECASE), "It really is a nice day!"),
            (re.compile(r"\bnice\s+day\b", re.IGNORECASE), "Yeah, it's a nice day!"),
            (re.compile(r"\bhow\s+about\s+this\s+weather\b", re.IGNORECASE), "The weather's been pretty interesting lately!"),
            (re.compile(r"\b(can\s+you\s+believe|hard\s+to\s+believe)\s+all\s+this\s+rain\b", re.IGNORECASE), "Yeah, it's been a lot of rain."),
            (re.compile(r"\blooks\s+like\s+it's\s+going\s+to\s+snow\b", re.IGNORECASE), "It does look like snow is coming."),
            (re.compile(r"\bcalling\s+for\s+thunderstorms\b", re.IGNORECASE), "Sounds like a stormy forecast."),
            (re.compile(r"\bdid\s+you\s+catch\s+the\s+news\b", re.IGNORECASE), "I saw some headlines—anything interesting?"),
            (re.compile(r"\bdid\s+you\s+hear\s+about\b", re.IGNORECASE), "I heard a bit—what did you hear?"),
            (re.compile(r"\blooking\s+forward\s+to\s+the\s+weekend\b", re.IGNORECASE), "Definitely. Any plans?"),
            (re.compile(r"\b(has\s+it\s+been|it's\s+been)\s+a\s+long\s+week\b", re.IGNORECASE), "It sure has—hopefully it winds down soon."),
            (re.compile(r"\bhow\s+busy\s+are\s+we\s+today\b", re.IGNORECASE), "It does feel a bit busy today."),
            (re.compile(r"\bare\s+you\s+enjoying\s+yourself\b", re.IGNORECASE), "Yeah, it's been nice! How about you?"),
            (re.compile(r"\bpretty\s+nice\s+place\b", re.IGNORECASE), "It is a nice place."),
            (re.compile(r"\bnice\s+day\s+to\s+be\s+outside\b", re.IGNORECASE), "Absolutely—great weather for it."),
            (re.compile(r"\bthe\s+bus\s+must\s+be\s+running\s+late\b", re.IGNORECASE), "Yeah, it seems delayed."),
            (re.compile(r"\bhow\s+long\s+have\s+you\s+been\s+waiting\b", re.IGNORECASE), "Not too long—how about you?"),
            (re.compile(r"\btest\b", re.IGNORECASE), "test succeeded"),
            (re.compile(r"\bwhat\s+is\s+your\s+name\b", re.IGNORECASE), "I'm AnalogAI."),
            (re.compile(r"\bwho\s+are\s+you\b", re.IGNORECASE), "I'm AnalogAI, your assistant."),
            (re.compile(r"\bwhat\s+can\s+you\s+do\b", re.IGNORECASE), "I can help answer questions and remember facts."),
            (re.compile(r"\bthank\s+you\b|\bthanks\b", re.IGNORECASE), "You're welcome!"),
        ]

    def respond(self, text: str) -> Optional[SmallTalkResponse]:
        if not text or not text.strip():
            return None
        cleaned = _normalize(text)
        if not _looks_like_small_talk(cleaned):
            return None
        for pattern, response in self._patterns:
            if pattern.search(cleaned):
                return SmallTalkResponse(response)
        return None


def _normalize(text: str) -> str:
    cleaned = re.sub(r"\s+", " ", text.strip())
    return cleaned


def _looks_like_small_talk(text: str) -> bool:
    if len(text) > 120:
        return False
    return True
