from __future__ import annotations

from analogai.foundation.common.logging.app_logger import app_logger


def detect_question_mark(text: str) -> bool:
    if not text:
        return False
    trimmed = text.strip()
    lowered = trimmed.lower()
    if "?" in trimmed:
        app_logger.info(
            f"[QuestionMarkDetector] detect_question_mark=True (reason=question_mark) text={trimmed!r}"
        )
        return True
    tokens = lowered.split()
    first_token = tokens[0] if tokens else ""
    question_words = {
        "who",
        "what",
        "when",
        "where",
        "why",
        "how",
        "which",
        "whom",
        "whose",
    }
    aux_starters = {
        "is",
        "are",
        "was",
        "were",
        "do",
        "does",
        "did",
        "can",
        "could",
        "would",
        "should",
        "will",
        "have",
        "has",
        "had",
        "may",
        "might",
        "must",
        "shall",
    }
    if first_token in question_words:
        app_logger.info(
            f"[QuestionMarkDetector] detect_question_mark=True (reason=question_word) text={trimmed!r}"
        )
        return True
    if first_token in aux_starters:
        app_logger.info(
            f"[QuestionMarkDetector] detect_question_mark=True (reason=aux_starter) text={trimmed!r}"
        )
        return True
    app_logger.info(
        f"[QuestionMarkDetector] detect_question_mark=False (reason=no_question_mark) text={trimmed!r}"
    )
    return False
