"""
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
    ) -> List[ResourceFilterResult]:
        """
        Filter resources that are available for download.

        Args:
            resources: List of ResearchResource objects to filter

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

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

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

        Args:
            resources: List of resources that were filtered

        Returns:
            FilterSummary object with detailed counts
        """
        results = self.filter_downloadable_resources(resources)
        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",
        )
