"""
Deep coverage tests for resource_filter.py.

Targets uncovered paths:
- filter_downloadable_resources: delegates to retry_manager
- get_filter_summary: delegates to retry_manager
- get_skipped_resources_info: categorization of skipped resources
"""

from datetime import timedelta
from unittest.mock import Mock, patch


class TestFilterDownloadableResources:
    """Cover filter_downloadable_resources paths."""

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_filter_returns_retry_manager_results(self, MockRetryManager):
        """filter_downloadable_resources returns retry-manager filter output."""
        from local_deep_research.library.download_management.filters.resource_filter import (
            ResourceFilter,
        )

        mock_rm = MockRetryManager.return_value
        mock_result = Mock(can_retry=True, resource_id=1)
        mock_rm.filter_resources.return_value = [mock_result]

        rf = ResourceFilter("testuser")
        results = rf.filter_downloadable_resources([Mock()])

        assert len(results) == 1
        mock_rm.filter_resources.assert_called_once()


class TestGetFilterSummary:
    """Cover get_filter_summary method."""

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_returns_filter_summary(self, MockRetryManager):
        """Returns FilterSummary from retry_manager."""
        from local_deep_research.library.download_management.filters.resource_filter import (
            ResourceFilter,
        )

        mock_rm = MockRetryManager.return_value
        mock_result = Mock(can_retry=True, resource_id=1)
        mock_rm.filter_resources.return_value = [mock_result]
        mock_summary = Mock(total=1, downloadable=1, skipped=0)
        mock_rm.get_filter_summary.return_value = mock_summary

        rf = ResourceFilter("testuser")
        summary = rf.get_filter_summary([Mock()])

        assert summary.total == 1
        mock_rm.get_filter_summary.assert_called_once()


class TestGetSkippedResourcesInfo:
    """Cover get_skipped_resources_info with categorization."""

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_categorizes_permanently_failed(self, MockRetryManager):
        """Categorizes permanently_failed resources."""
        from local_deep_research.library.download_management.filters.resource_filter import (
            ResourceFilter,
        )

        mock_rm = MockRetryManager.return_value
        mock_result = Mock(
            can_retry=False,
            resource_id=42,
            status="permanently_failed",
            reason="Max retries exceeded",
            estimated_wait=None,
        )
        mock_rm.filter_resources.return_value = [mock_result]
        mock_rm.status_tracker.get_resource_status.return_value = {
            "attempt_count": 5
        }

        rf = ResourceFilter("testuser")
        info = rf.get_skipped_resources_info([Mock()])

        assert info["total_skipped"] == 1
        assert len(info["permanently_failed"]) == 1
        assert len(info["temporarily_failed"]) == 0
        assert info["permanently_failed"][0]["resource_id"] == 42
        assert info["permanently_failed"][0]["estimated_wait_minutes"] is None

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_categorizes_temporarily_failed(self, MockRetryManager):
        """Categorizes temporarily_failed resources with wait time."""
        from local_deep_research.library.download_management.filters.resource_filter import (
            ResourceFilter,
        )

        mock_rm = MockRetryManager.return_value
        mock_result = Mock(
            can_retry=False,
            resource_id=99,
            status="temporarily_failed",
            reason="Rate limited",
            estimated_wait=timedelta(minutes=5),
        )
        mock_rm.filter_resources.return_value = [mock_result]
        mock_rm.status_tracker.get_resource_status.return_value = {
            "attempt_count": 2
        }

        rf = ResourceFilter("testuser")
        info = rf.get_skipped_resources_info([Mock()])

        assert info["total_skipped"] == 1
        assert len(info["temporarily_failed"]) == 1
        assert info["temporarily_failed"][0]["estimated_wait_minutes"] == 5.0

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_categorizes_other_skipped(self, MockRetryManager):
        """Categorizes resources with unknown status."""
        from local_deep_research.library.download_management.filters.resource_filter import (
            ResourceFilter,
        )

        mock_rm = MockRetryManager.return_value
        mock_result = Mock(
            can_retry=False,
            resource_id=7,
            status="cooldown",
            reason="Cooling down",
            estimated_wait=timedelta(seconds=120),
        )
        mock_rm.filter_resources.return_value = [mock_result]
        mock_rm.status_tracker.get_resource_status.return_value = {}

        rf = ResourceFilter("testuser")
        info = rf.get_skipped_resources_info([Mock()])

        assert info["total_skipped"] == 1
        assert len(info["other_skipped"]) == 1

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_mixed_results_skips_retryable(self, MockRetryManager):
        """Only includes non-retryable resources in skipped info."""
        from local_deep_research.library.download_management.filters.resource_filter import (
            ResourceFilter,
        )

        mock_rm = MockRetryManager.return_value
        retryable = Mock(can_retry=True, resource_id=1)
        not_retryable = Mock(
            can_retry=False,
            resource_id=2,
            status="permanently_failed",
            reason="gone",
            estimated_wait=None,
        )
        mock_rm.filter_resources.return_value = [retryable, not_retryable]
        mock_rm.status_tracker.get_resource_status.return_value = {}

        rf = ResourceFilter("testuser")
        info = rf.get_skipped_resources_info([Mock(), Mock()])

        assert info["total_skipped"] == 1
