"""
Day count calculator for time-shifting operations.

Handles leap years and month-specific day counts for filtering records
during date transformation.
"""

from calendar import monthrange


class DayCountCalculator:
    """
    Calculates valid days for target months.

    Handles leap years correctly when transforming 31-day source months
    to shorter target months (28, 29, or 30 days).
    """

    def get_valid_days(self, year: int, month: int) -> int:
        """
        Get number of days in the specified month.

        Args:
            year: Target year (e.g., 2026)
            month: Target month (1-12)

        Returns:
            Number of days in the month (28-31)

        Examples:
            >>> calc = DayCountCalculator()
            >>> calc.get_valid_days(2024, 2)  # Leap year February
            29
            >>> calc.get_valid_days(2025, 2)  # Non-leap February
            28
            >>> calc.get_valid_days(2026, 4)  # April
            30
        """
        return monthrange(year, month)[1]

    def should_include_record(self, day: int, year: int, month: int) -> bool:
        """
        Check if a record's day is valid for the target month.

        Used to filter out records that would have invalid dates in
        the target month (e.g., day 31 when transforming to February).

        Args:
            day: Day of month from source data (1-31)
            year: Target year
            month: Target month (1-12)

        Returns:
            True if day is valid for target month, False otherwise

        Examples:
            >>> calc = DayCountCalculator()
            >>> calc.should_include_record(31, 2026, 2)  # Day 31 in Feb
            False
            >>> calc.should_include_record(28, 2026, 2)  # Day 28 in Feb
            True
            >>> calc.should_include_record(31, 2026, 1)  # Day 31 in Jan
            True
        """
        return day <= self.get_valid_days(year, month)
