"""
Resource Filter

Smart filtering logic for downloadable resources based on failure history,
cooldowns, and retry policies. Replaces simple file existence checks with
intelligent retry management.
"""

from typing import List, Optional

from loguru import logger

from ..retry_manager import RetryManager, ResourceFilterResult, FilterSummary


class ResourceFilter:
    """Filter resources for download based on history and policies"""

    def __init__(self, username: str, password: Optional[str] = None):
        """
        Initialize the resource filter.

        Args:
            username: Username for database access
            password: Optional password for encrypted database
        """
        self.username = username
        self.retry_manager = RetryManager(username, password)
        logger.info(f"Initialized for user: {username}")

    def filter_downloadable_resources(
        self, resources: List, check_files: bool = True
    ) -> List[ResourceFilterResult]:
        """
        Filter resources that are available for download.

        Args:
            resources: List of ResearchResource objects to filter
            check_files: Whether to also check for existing text files (legacy behavior)

        Returns:
            List of ResourceFilterResult objects with retry decisions
        """
        logger.info(f"Filtering {len(resources)} resources")

        # Use retry manager to filter based on failure history
        results = self.retry_manager.filter_resources(resources)

        # Optional legacy file existence check
        if check_files:
            results = self._apply_legacy_file_check(results)

        return results

    def _apply_legacy_file_check(
        self, results: List[ResourceFilterResult]
    ) -> List[ResourceFilterResult]:
        """
        Apply legacy file existence checking to filter results.

        Args:
            results: Existing filter results to modify

        Returns:
            Updated filter results with file existence check
        """
        # This would get the download service instance to check for existing files
        # For now, we'll skip this as the retry manager handles the main logic
        return results

    def get_filter_summary(
        self, resources: List, check_files: bool = True
    ) -> FilterSummary:
        """
        Get a summary of filtering results.

        Args:
            resources: List of resources that were filtered
            check_files: Whether file existence checking was applied

        Returns:
            FilterSummary object with detailed counts
        """
        results = self.filter_downloadable_resources(resources, check_files)
        return self.retry_manager.get_filter_summary(results)

    def get_skipped_resources_info(self, resources: List) -> dict:
        """
        Get detailed information about skipped resources for UI display.

        Args:
            resources: List of all resources

        Returns:
            Dictionary with detailed skip information
        """
        results = self.filter_downloadable_resources(resources)

        skipped_resources = []
        for result in results:
            if not result.can_retry:
                status_info = (
                    self.retry_manager.status_tracker.get_resource_status(
                        result.resource_id
                    )
                )
                skipped_resources.append(
                    {
                        "resource_id": result.resource_id,
                        "status": result.status,
                        "reason": result.reason,
                        "estimated_wait_minutes": result.estimated_wait.total_seconds()
                        // 60
                        if result.estimated_wait
                        else None,
                        "status_info": status_info,
                    }
                )

        return {
            "total_skipped": len(skipped_resources),
            "permanently_failed": [
                r
                for r in skipped_resources
                if r["status"] == "permanently_failed"
            ],
            "temporarily_failed": [
                r
                for r in skipped_resources
                if r["status"] == "temporarily_failed"
            ],
            "other_skipped": [
                r
                for r in skipped_resources
                if r["status"]
                not in ["permanently_failed", "temporarily_failed"]
            ],
            "skipped_resources": skipped_resources,
        }

    def should_skip_resource(self, resource_id: int) -> tuple[bool, str]:
        """
        Quick check if a specific resource should be skipped.

        Args:
            resource_id: Resource identifier

        Returns:
            Tuple of (should_skip, reason)
        """
        decision = self.retry_manager.should_retry_resource(resource_id)
        return (
            not decision.can_retry,
            decision.reason or "Resource not available for retry",
        )

    def get_retry_statistics(self) -> dict:
        """
        Get retry statistics for monitoring and debugging.

        Returns:
            Dictionary with retry statistics
        """
        return self.retry_manager.get_retry_statistics()
