"""
Comprehensive tests for core/utils.py module.
Tests utility functions like utc_now, date formatting, and helpers.
"""

from datetime import datetime, timezone, timedelta


class TestUtcNow:
    """Tests for utc_now function."""

    def test_returns_datetime(self):
        """Test returns a datetime object."""
        from local_deep_research.news.core.utils import utc_now

        result = utc_now()

        assert isinstance(result, datetime)

    def test_returns_utc_timezone(self):
        """Test returns datetime with UTC timezone."""
        from local_deep_research.news.core.utils import utc_now

        result = utc_now()

        assert result.tzinfo == timezone.utc

    def test_is_close_to_current_time(self):
        """Test result is close to current time."""
        from local_deep_research.news.core.utils import utc_now

        before = datetime.now(timezone.utc)
        result = utc_now()
        after = datetime.now(timezone.utc)

        assert before <= result <= after

    def test_is_not_naive(self):
        """Test datetime is timezone-aware, not naive."""
        from local_deep_research.news.core.utils import utc_now

        result = utc_now()

        assert result.tzinfo is not None


class TestFormatTimestamp:
    """Tests for format_timestamp function if it exists."""

    def test_can_import_utc_now(self):
        """Test utc_now can be imported."""
        from local_deep_research.news.core.utils import utc_now

        assert utc_now is not None
        assert callable(utc_now)


class TestDateTimeHelpers:
    """Tests for datetime helper functions."""

    def test_utc_now_multiple_calls_are_sequential(self):
        """Test multiple utc_now calls return sequential times."""
        from local_deep_research.news.core.utils import utc_now
        import time

        first = utc_now()
        time.sleep(0.001)  # Small delay
        second = utc_now()

        assert first <= second

    def test_utc_now_returns_different_instances(self):
        """Test utc_now returns different instances each call."""
        from local_deep_research.news.core.utils import utc_now

        first = utc_now()
        second = utc_now()

        # They should be different objects
        assert first is not second

    def test_utc_now_has_all_datetime_attributes(self):
        """Test utc_now result has all datetime attributes."""
        from local_deep_research.news.core.utils import utc_now

        result = utc_now()

        assert hasattr(result, "year")
        assert hasattr(result, "month")
        assert hasattr(result, "day")
        assert hasattr(result, "hour")
        assert hasattr(result, "minute")
        assert hasattr(result, "second")
        assert hasattr(result, "microsecond")

    def test_utc_now_isoformat_works(self):
        """Test utc_now result can be converted to ISO format."""
        from local_deep_research.news.core.utils import utc_now

        result = utc_now()
        iso = result.isoformat()

        assert isinstance(iso, str)
        assert "T" in iso  # Has datetime separator
        assert "+" in iso or "Z" in iso  # Has timezone info

    def test_utc_now_can_be_subtracted(self):
        """Test utc_now result can be used in time calculations."""
        from local_deep_research.news.core.utils import utc_now

        now = utc_now()
        delta = timedelta(hours=1)
        past = now - delta

        assert past < now

    def test_utc_now_can_compare_with_datetime(self):
        """Test utc_now result can compare with datetime."""
        from local_deep_research.news.core.utils import utc_now

        now = utc_now()
        other = datetime.now(timezone.utc)

        # Should be able to compare
        _ = now < other or now > other or now == other

    def test_utc_now_timestamp_method(self):
        """Test utc_now result has timestamp method."""
        from local_deep_research.news.core.utils import utc_now

        result = utc_now()
        timestamp = result.timestamp()

        assert isinstance(timestamp, float)
        assert timestamp > 0


class TestUtilsImports:
    """Tests for module imports."""

    def test_utc_now_is_callable(self):
        """Test utc_now is callable."""
        from local_deep_research.news.core.utils import utc_now

        assert callable(utc_now)


class TestUtilsEdgeCases:
    """Edge case tests for utils functions."""

    def test_utc_now_handles_rapid_calls(self):
        """Test utc_now handles rapid sequential calls."""
        from local_deep_research.news.core.utils import utc_now

        results = [utc_now() for _ in range(100)]

        # All should be datetime objects
        assert all(isinstance(r, datetime) for r in results)

        # Should be in non-decreasing order
        for i in range(1, len(results)):
            assert results[i] >= results[i - 1]

    def test_utc_now_year_is_reasonable(self):
        """Test utc_now returns reasonable year."""
        from local_deep_research.news.core.utils import utc_now

        result = utc_now()

        # Should be between 2024 and 2100 (reasonable for this codebase)
        assert 2024 <= result.year <= 2100

    def test_utc_now_works_in_list_comprehension(self):
        """Test utc_now works correctly in list comprehensions."""
        from local_deep_research.news.core.utils import utc_now

        timestamps = [utc_now().isoformat() for _ in range(5)]

        assert len(timestamps) == 5
        assert all(isinstance(t, str) for t in timestamps)
