"""
Tests for the download management system.

Tests cover:
- Failure classification by HTTP status code
- Rate limit failure handling
- ArXiv reCAPTCHA detection
- FilterSummary aggregation
"""

from datetime import timedelta


class TestFailureClassifier:
    """Tests for the FailureClassifier class."""

    def test_failure_classifier_http_404(self):
        """Classify 404 as PermanentFailure."""
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
            PermanentFailure,
        )

        classifier = FailureClassifier()

        result = classifier.classify_failure(
            error_type="HTTPError",
            status_code=404,
            url="https://example.com/missing",
        )

        assert isinstance(result, PermanentFailure)
        assert result.is_permanent() is True
        assert "404" in result.message or "not found" in result.message.lower()

    def test_failure_classifier_http_429(self):
        """Classify 429 as RateLimitFailure."""
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
            RateLimitFailure,
        )

        classifier = FailureClassifier()

        result = classifier.classify_failure(
            error_type="HTTPError",
            status_code=429,
            url="https://arxiv.org/pdf/123",
            details="Too many requests",
        )

        assert isinstance(result, RateLimitFailure)
        assert result.is_permanent() is False
        assert result.domain == "arxiv.org"

    def test_failure_classifier_http_403(self):
        """Classify 403 as PermanentFailure."""
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
            PermanentFailure,
        )

        classifier = FailureClassifier()

        result = classifier.classify_failure(
            error_type="HTTPError",
            status_code=403,
            url="https://example.com/forbidden",
        )

        assert isinstance(result, PermanentFailure)
        assert result.is_permanent() is True
        assert "403" in result.message or "forbidden" in result.message.lower()

    def test_failure_classifier_arxiv_recaptcha(self):
        """ArXiv reCAPTCHA as 3-day temporary failure."""
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
            TemporaryFailure,
        )

        classifier = FailureClassifier()

        result = classifier.classify_failure(
            error_type="ContentError",
            url="https://arxiv.org/pdf/123",
            details="arXiv returned a reCAPTCHA challenge page",
        )

        assert isinstance(result, TemporaryFailure)
        assert result.is_permanent() is False
        assert result.retry_after == timedelta(days=3)
        assert (
            "recaptcha" in result.error_type.lower()
            or "recaptcha" in result.message.lower()
        )

    def test_failure_classifier_timeout(self):
        """Timeout errors should be temporary."""
        from local_deep_research.library.download_management.failure_classifier import (
            FailureClassifier,
            TemporaryFailure,
        )

        classifier = FailureClassifier()

        result = classifier.classify_failure(
            error_type="TimeoutError",
            url="https://example.com/slow",
            details="Request timed out after 30 seconds",
        )

        assert isinstance(result, TemporaryFailure)
        assert result.is_permanent() is False


class TestFilterSummary:
    """Tests for FilterSummary aggregation."""

    def test_filter_summary_aggregation(self):
        """FilterSummary correctly aggregates results."""
        from local_deep_research.library.download_management.retry_manager import (
            FilterSummary,
            ResourceFilterResult,
        )

        summary = FilterSummary()

        # Add various results
        results = [
            ResourceFilterResult(
                resource_id=1,
                can_retry=True,
                status="available",
                reason="",
            ),
            ResourceFilterResult(
                resource_id=2,
                can_retry=True,
                status="available",
                reason="",
            ),
            ResourceFilterResult(
                resource_id=3,
                can_retry=False,
                status="permanently_failed",
                reason="404 not found",
            ),
            ResourceFilterResult(
                resource_id=4,
                can_retry=False,
                status="temporarily_failed",
                reason="In cooldown",
            ),
        ]

        for result in results:
            summary.add_result(result)

        assert summary.total_count == 4
        assert summary.downloadable_count == 2
        assert summary.permanently_failed_count == 1
        assert summary.temporarily_failed_count == 1

    def test_filter_summary_to_dict(self):
        """FilterSummary.to_dict() returns correct structure."""
        from local_deep_research.library.download_management.retry_manager import (
            FilterSummary,
            ResourceFilterResult,
        )

        summary = FilterSummary()
        summary.add_result(
            ResourceFilterResult(
                resource_id=1,
                can_retry=True,
                status="available",
                reason="",
            )
        )

        result = summary.to_dict()

        assert "total_count" in result
        assert "downloadable_count" in result
        assert "permanently_failed_count" in result
        assert "temporarily_failed_count" in result
        assert "available_count" in result
        assert "failure_type_counts" in result
        assert result["total_count"] == 1
        assert result["downloadable_count"] == 1
