from datetime import datetime
from .numbers import num_to_word

# Mapping of month names in Hebrew with diacritics (Gregorian months)
MONTHS = {
    1: "יָ֫נוּאָר",
    2: "פֶ֫בְרוּאָר",
    3: "מֵ֫רְץ",
    4: "אֵפְרִיל",
    5: "מַאי",
    6: "י֫וּנִי",
    7: "י֫וּלִי",
    8: "א֫וֹגֻסְט",
    9: "סֶפְּטֶ֫מְבֶּר",
    10: "אוֹקְט֫וֹבֶּר",
    11: "נוֹבֶ֫מְבֶּר",
    12: "דֶּצֶ֫מְבֶּר",
}

# Mapping of day names in Hebrew with diacritics
DAYS = {
    0: "יוֹם רִאשׁוֹן",
    1: "יוֹם שֵׁנִי",
    2: "יוֹם שְׁלִישִׁי",
    3: "יוֹם רֵבִיעִי",
    4: "יוֹם חֲמִישִׁי",
    5: "יוֹם שִׁישִׁי",
    6: "יוֹם שַׁבָּת",
}


def date_to_word(word: str, include_day_name=False) -> str:
    """
    Converts a given date string in formats (YYYY-MM-DD, YYYY.MM.DD, YYYY/MM/DD) to Hebrew date format with diacritics.
    Returns the original word if it's not a valid date.
    """
    separators = ["-", ".", "/"]
    orders = [("%Y", "%m", "%d"), ("%d", "%m", "%Y")]
    date_formats = [sep.join(order) for order in orders for sep in separators]

    for date_format in date_formats:
        try:
            # Try parsing the word with each date format
            date_obj = datetime.strptime(word, date_format)

            # Get the Hebrew day name with diacritics
            day_name = DAYS[date_obj.weekday()]

            # Convert month to Hebrew name with diacritics
            month_name = MONTHS[date_obj.month]
            day = num_to_word(str(date_obj.day))
            year = num_to_word(str(date_obj.year))

            text = f"{day} בֵּ{month_name} {year}"
            if include_day_name:
                text = f"{day_name}, {text}"
            return text
        except ValueError:
            continue
    return word
