from datetime import date

import pytest

from ccma.services.intervals import (
    IntervalValidationError,
    normalize_anniversary_intervals,
    parse_anniversary_intervals,
)


def test_intervals_accept_commas_semicolons_and_units() -> None:
    intervals = parse_anniversary_intervals("30d, 2M;1;10Y;10y")
    assert [interval.token for interval in intervals] == ["30D", "2M", "1Y", "10Y"]
    assert normalize_anniversary_intervals("30d, 2M;1Y") == "30D;2M;1Y"


def test_month_and_year_intervals_use_calendar_arithmetic() -> None:
    intervals = parse_anniversary_intervals("1M;1Y")
    assert intervals[0].target_date(date(2024, 1, 31)) == date(2024, 2, 29)
    assert intervals[1].target_date(date(2024, 2, 29)) == date(2025, 2, 28)


@pytest.mark.parametrize("value", ["", "D", "0D", "-1Y", "101Y"])
def test_invalid_intervals_are_rejected(value) -> None:
    with pytest.raises(IntervalValidationError):
        parse_anniversary_intervals(value)
