"""
Rule-based comment sentiment classifier.

No external dependencies — uses only the ``re`` module.  Returns −1
(negative), 0 (neutral), or 1 (positive) for a given comment string.
"""

import re

POSITIVE_WORDS = frozenset({
    "love", "great", "awesome", "amazing", "excellent", "fantastic",
    "wonderful", "beautiful", "perfect", "brilliant", "outstanding",
    "superb", "incredible", "magnificent", "thank", "thanks", "good",
    "best", "cool", "nice", "happy", "enjoy", "enjoyed", "helpful",
    "fire", "impressive", "solid", "recommend", "recommended",
    "favorite", "favourite", "gem", "inspiring", "inspirational",
    "masterpiece", "phenomenal", "stellar", "bravo", "wow",
    "delightful", "exceptional", "glorious", "dope",
})

NEGATIVE_WORDS = frozenset({
    "bad", "terrible", "awful", "horrible", "worst", "hate",
    "disappointing", "disappointed", "poor", "boring", "waste",
    "ugly", "broken", "useless", "annoying", "frustrating",
    "mediocre", "overpriced", "scam", "trash", "garbage",
    "rubbish", "dreadful", "pathetic", "lame", "sucks",
    "meh", "bland", "uninspired", "weak", "confusing",
    "disgusting", "offensive", "painful", "regret", "refund",
    "spam", "fake", "ripoff", "avoid", "lackluster",
})

NEGATION_WORDS = frozenset({
    "not", "no", "never", "neither", "nor", "nothing",
    "nowhere", "hardly", "barely", "scarcely", "dont",
    "doesnt", "didnt", "wasnt", "werent", "wont",
    "wouldnt", "couldnt", "shouldnt", "isnt", "arent",
    "cant", "cannot",
})

INTENSIFIERS = frozenset({
    "very", "really", "extremely", "absolutely", "incredibly",
    "totally", "completely", "utterly", "super", "so",
})

_TOKEN_RE = re.compile(r"[a-zA-Z]{2,}")
_NEGATION_WINDOW = 3


def classify_sentiment(text):
    """Classify *text* and return ``-1``, ``0``, or ``1``."""
    if not text:
        return 0

    tokens = [t.lower() for t in _TOKEN_RE.findall(text)]
    if not tokens:
        return 0

    score = 0.0
    negation_countdown = 0
    intensifier_active = False

    for token in tokens:
        # Strip trailing apostrophe contractions already handled by regex
        clean = token.replace("'", "")

        if clean in NEGATION_WORDS:
            negation_countdown = _NEGATION_WINDOW
            continue

        if clean in INTENSIFIERS:
            intensifier_active = True
            continue

        weight = 0.0
        if clean in POSITIVE_WORDS:
            weight = 1.0
        elif clean in NEGATIVE_WORDS:
            weight = -1.0

        if weight != 0.0:
            if intensifier_active:
                weight *= 1.5
            if negation_countdown > 0:
                weight = -weight
            score += weight

        intensifier_active = False
        if negation_countdown > 0:
            negation_countdown -= 1

    # Normalize by token count to avoid bias toward long comments
    normalized = score / len(tokens)

    if normalized > 0.04:
        return 1
    elif normalized < -0.04:
        return -1
    return 0
