"""
Deep coverage tests for resource_filter.py.

Targets uncovered paths:
- filter_downloadable_resources: with check_files=True (legacy path)
- get_filter_summary: delegates to retry_manager
- get_skipped_resources_info: categorization of skipped resources
- get_retry_statistics: delegates to retry_manager
"""

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_with_check_files_true(self, MockRetryManager):
        """Calls _apply_legacy_file_check when check_files=True."""
        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()], check_files=True)

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

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_filter_with_check_files_false(self, MockRetryManager):
        """Skips _apply_legacy_file_check when check_files=False."""
        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()], check_files=False)

        assert len(results) == 1


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


class TestGetRetryStatistics:
    """Cover get_retry_statistics delegation."""

    @patch(
        "local_deep_research.library.download_management.filters.resource_filter.RetryManager"
    )
    def test_delegates_to_retry_manager(self, MockRetryManager):
        """get_retry_statistics returns retry_manager stats."""
        from local_deep_research.library.download_management.filters.resource_filter import (
            ResourceFilter,
        )

        mock_rm = MockRetryManager.return_value
        mock_rm.get_retry_statistics.return_value = {
            "total_retries": 10,
            "success_rate": 0.8,
        }

        rf = ResourceFilter("testuser")
        stats = rf.get_retry_statistics()

        assert stats["total_retries"] == 10
        assert stats["success_rate"] == 0.8
