"""Date ranges with a bare MONTH on an end, and "N months including <month>".

Reported live (attendance, today 29 Sep 2026):
  * "please show me attdeance for 3 month including septmeber" -> Jun 01 to
    Aug 31: nothing matched, and the LLM fallback read it as "the last 3
    months", dropping the very month asked to be included.
  * "show my attdeance for 1 july to septmeber" -> Jul 01 to Jul 01: the
    single-date matcher claimed "1 july" and the range was lost.
  * (and "july to september" came back as July alone.)

Spelling was not the cause — the correctly spelled phrases failed the same
way — but "septmeber" is folded too, so it can't break them again.
"""
from datetime import date

import pytest

import services.date_query_resolver as dq

TODAY = date(2026, 9, 29)


def _range(query):
    r = dq.resolve(query, TODAY)
    assert isinstance(r, dict) and r.get("type") == "range", f"{query!r} -> {r}"
    return r["start_date"], r["end_date"]


class TestReported:
    def test_three_months_including_september(self):
        assert _range("please show me attdeance for 3 month including septmeber") == (
            date(2026, 7, 1), TODAY)

    def test_a_day_to_a_bare_month(self):
        assert _range("show my attdeance for 1 july to septmeber'") == (date(2026, 7, 1), TODAY)

    def test_month_to_month(self):
        assert _range("attendance from july to september") == (date(2026, 7, 1), TODAY)


class TestMonthSpan:
    @pytest.mark.parametrize("query,start,end", [
        ("july to september", date(2026, 7, 1), TODAY),                 # stops at today
        ("july to august", date(2026, 7, 1), date(2026, 8, 31)),        # a past month runs to its end
        ("july to 15 september", date(2026, 7, 1), date(2026, 9, 15)),
        ("5 july till august", date(2026, 7, 5), date(2026, 8, 31)),
        ("july 5 to august", date(2026, 7, 5), date(2026, 8, 31)),
        ("between july and september", date(2026, 7, 1), TODAY),
        ("jul - aug", date(2026, 7, 1), date(2026, 8, 31)),
        ("november to february", date(2025, 11, 1), date(2026, 2, 28)),  # wraps: starts the year before
        ("attendance july to august 2025", date(2025, 7, 1), date(2025, 8, 31)),
    ])
    def test_spans(self, query, start, end):
        assert _range(query) == (start, end)


class TestNMonthsEnding:
    @pytest.mark.parametrize("query,start,end", [
        ("3 months including september", date(2026, 7, 1), TODAY),
        ("three months including september", date(2026, 7, 1), TODAY),
        ("last 3 months including this month", date(2026, 7, 1), TODAY),
        ("attendance for 3 months including current month", date(2026, 7, 1), TODAY),
        ("2 months till august", date(2026, 7, 1), date(2026, 8, 31)),
        ("4 months up to march", date(2025, 12, 1), date(2026, 3, 31)),   # crosses the year
    ])
    def test_n_months_ending_at_a_month(self, query, start, end):
        assert _range(query) == (start, end)


class TestUnchanged:
    @pytest.mark.parametrize("query,start,end", [
        ("attendance for 1 july to 30 september", date(2026, 7, 1), date(2026, 9, 30)),
        ("attendance for last 3 months", date(2026, 6, 1), date(2026, 8, 31)),   # completed months
        ("attendance for september", date(2026, 9, 1), date(2026, 9, 30)),
        ("from 20 august", date(2026, 8, 20), TODAY),
    ])
    def test_ranges(self, query, start, end):
        assert _range(query) == (start, end)

    @pytest.mark.parametrize("query,day", [
        ("23 aug", date(2026, 8, 23)), ("punch in on 5 sept", date(2026, 9, 5)),
    ])
    def test_single_dates(self, query, day):
        assert dq.resolve(query, TODAY) == {"type": "single", "date": day}


class TestMonthTypos:
    @pytest.mark.parametrize("typo,month", [
        ("septmeber", "september"), ("septembr", "september"), ("agust", "august"),
        ("novmber", "november"), ("decmber", "december"), ("febuary", "february"),
        ("octobr", "october"),
    ])
    def test_folded(self, typo, month):
        assert dq.fold_month_typos(f"attendance for {typo}") == f"attendance for {month}"

    @pytest.mark.parametrize("word", ["number", "member", "remember", "junior", "decent", "attendance"])
    def test_ordinary_words_are_left_alone(self, word):
        assert dq.fold_month_typos(f"my {word}") == f"my {word}"
