"""Core caffeinate_me() function — backward-compatible entry point."""

import random
import re
from typing import Optional

from .quotes import get_random_quote


def caffeinate_me(mood: Optional[str] = None, no_emoji: bool = False) -> str:
    """
    Decide whether it's time for another cup of coffee.

    An 80 % chance returns a witty coffee quote; a 20 % chance suggests a nap.

    Args:
        mood: Optional mood filter ('energetic', 'sleepy', 'stressed', 'zen').
        no_emoji: When True, emoji are stripped from the output.

    Returns:
        A humorous message about your caffeine cravings.
    """
    if random.random() < 0.8:
        quote = get_random_quote(mood=mood)
        result = f"{quote} Time for another cup!"
    else:
        result = "Maybe just a nap instead? \U0001f914"

    return strip_emoji(result) if no_emoji else result


def strip_emoji(text: str) -> str:
    """Remove emoji characters from *text*."""
    pattern = re.compile(
        "["
        "\U0001F600-\U0001F64F"
        "\U0001F300-\U0001F5FF"
        "\U0001F680-\U0001F6FF"
        "\U0001F1E0-\U0001F1FF"
        "\U00002700-\U000027BF"
        "\U000024C2-\U0001F251"
        "\U0001F900-\U0001F9FF"
        "\U0001FA00-\U0001FA7F"
        "\u2615"  # hot beverage
        "]+",
        flags=re.UNICODE,
    )
    return pattern.sub("", text).strip()
