"""Test milestone computation — verify 2/3/5/7/10 year windows."""

import pytest
from capabilities.harvest import _compute_milestone_metadata
from datetime import date, timedelta


class TestMilestoneComputation:
    """Test milestone metadata computation."""
    
    def test_exactly_5_years(self):
        """Lead filed exactly 5 years ago should hit 5yr milestone."""
        today = date(2026, 5, 20)
        filing_date = date(2021, 5, 20)
        
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        assert meta["milestone_year"] == 5
        assert meta["milestone_window_hit"] == True
        assert abs(meta["milestone_proximity_days"]) <= 1  # Allow 1 day rounding
    
    def test_5_years_plus_30_days(self):
        """Lead filed 5 years and 30 days ago should hit 5yr milestone."""
        today = date(2026, 5, 20)
        filing_date = today - timedelta(days=5*365.25 + 30)
        
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        assert meta["milestone_year"] == 5
        assert meta["milestone_window_hit"] == True
        assert meta["milestone_proximity_days"] >= -90
    
    def test_5_years_plus_100_days(self):
        """Lead filed 5 years and 100 days ago should NOT hit milestone (outside ±90 window)."""
        today = date(2026, 5, 20)
        filing_date = today - timedelta(days=5*365.25 + 100)
        
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        # Outside window
        assert meta["milestone_window_hit"] == False
        assert meta["milestone_year"] is None
        # But proximity calculation should still work
        assert meta["milestone_proximity_days"] is not None
    
    def test_3_year_milestone(self):
        """Lead filed ~3 years ago should hit 3yr milestone."""
        today = date(2026, 5, 20)
        filing_date = date(2023, 5, 25)  # ~3 years ago
        
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        assert meta["milestone_year"] == 3
        assert meta["milestone_window_hit"] == True
    
    def test_7_year_milestone(self):
        """Lead filed ~7 years ago should hit 7yr milestone."""
        today = date(2026, 5, 20)
        filing_date = date(2019, 5, 15)  # ~7 years ago
        
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        assert meta["milestone_year"] == 7
        assert meta["milestone_window_hit"] == True
    
    def test_10_year_milestone(self):
        """Lead filed ~10 years ago should hit 10yr milestone."""
        today = date(2026, 5, 20)
        filing_date = date(2016, 5, 20)  # ~10 years ago
        
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        assert meta["milestone_year"] == 10
        assert meta["milestone_window_hit"] == True
    
    def test_between_milestones(self):
        """Lead filed 4 years ago (between 3 and 5) should not hit milestone."""
        today = date(2026, 5, 20)
        filing_date = date(2022, 5, 20)  # 4 years ago
        
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        # 4 years is not within ±90 days of 3yr or 5yr milestone
        assert meta["milestone_window_hit"] == False
        assert meta["milestone_year"] is None
    
    def test_leap_year_feb29(self):
        """Feb 29 filing dates should handle leap year edge case."""
        today = date(2026, 2, 28)
        filing_date = date(2020, 2, 29)  # Leap year
        
        # This should not crash
        meta = _compute_milestone_metadata(filing_date.isoformat(), today)
        
        assert "milestone_year" in meta
        assert "milestone_window_hit" in meta
        assert "milestone_proximity_days" in meta