"""Test day-mode ouroboros boundary logic and consistency.

Migrated from monthly ouroboros tests (Issue #97) to day-only mode.

Tests cover:
- Day boundary detection for _is_ouroboros_boundary
- Day segment iteration via get_ouroboros_boundaries and iter_ouroboros_segments
- Legacy mode rejection
"""

from datetime import date

import pytest
from opendeviationbar.checkpoint import _is_ouroboros_boundary
from opendeviationbar.ouroboros import (
    get_ouroboros_boundaries,
    iter_ouroboros_segments,
)


class TestDayBoundaryDetection:
    """Test _is_ouroboros_boundary returns True for all dates in day mode."""

    @pytest.mark.parametrize("day", range(1, 29))
    def test_every_day_is_boundary(self, day: int):
        """Every day is a boundary in day mode."""
        date_str = f"2024-02-{day:02d}"
        assert _is_ouroboros_boundary(date_str, "day") is True

    def test_jan1_is_boundary(self):
        """Jan 1 is a day boundary."""
        assert _is_ouroboros_boundary("2024-01-01", "day") is True

    def test_dec31_is_boundary(self):
        """Dec 31 is a day boundary."""
        assert _is_ouroboros_boundary("2024-12-31", "day") is True


class TestLegacyModeRejection:
    """Test that legacy modes raise ValueError in _is_ouroboros_boundary."""

    def test_month_mode_rejected(self):
        """Month mode raises ValueError."""
        with pytest.raises(ValueError, match="Only 'day' mode is supported"):
            _is_ouroboros_boundary("2024-01-01", "month")

    def test_year_mode_rejected(self):
        """Year mode raises ValueError."""
        with pytest.raises(ValueError, match="Only 'day' mode is supported"):
            _is_ouroboros_boundary("2024-01-01", "year")

    def test_week_mode_rejected(self):
        """Week mode raises ValueError."""
        with pytest.raises(ValueError, match="Only 'day' mode is supported"):
            _is_ouroboros_boundary("2024-01-01", "week")


class TestDaySegmentIteration:
    """Test iter_ouroboros_segments produces correct date ranges for day mode."""

    def test_three_day_span(self):
        """Three-day span produces 3 segments."""
        segments = list(iter_ouroboros_segments(
            date(2024, 3, 1), date(2024, 3, 3), "day",
        ))
        assert len(segments) == 3

    def test_cross_month_boundary(self):
        """Day segments cross month boundary seamlessly."""
        segments = list(iter_ouroboros_segments(
            date(2024, 1, 31), date(2024, 2, 2), "day",
        ))
        assert len(segments) == 3

    def test_full_month(self):
        """Full month produces correct number of segments."""
        segments = list(iter_ouroboros_segments(
            date(2024, 2, 1), date(2024, 2, 29), "day",
        ))
        assert len(segments) == 29  # Leap year

    def test_single_day(self):
        """Single day produces 1 segment."""
        segments = list(iter_ouroboros_segments(
            date(2024, 6, 15), date(2024, 6, 15), "day",
        ))
        assert len(segments) == 1


class TestDayBoundaries:
    """Test get_ouroboros_boundaries for day mode edge cases."""

    def test_cross_year_boundary(self):
        """Day boundaries cross year boundary correctly."""
        boundaries = get_ouroboros_boundaries(
            date(2023, 12, 30), date(2024, 1, 2), "day",
        )
        assert len(boundaries) == 4

    def test_leap_day(self):
        """Feb 29 in leap year is a valid day boundary."""
        boundaries = get_ouroboros_boundaries(
            date(2024, 2, 28), date(2024, 3, 1), "day",
        )
        assert len(boundaries) == 3
