"""
Document scheduler utility for automatic processing of research history.
This utility class interfaces with the NewsScheduler to provide document processing functionality.
"""

from typing import Any, Dict

from loguru import logger
from ..news.subscription_manager.scheduler import get_news_scheduler


class DocumentSchedulerUtil:
    """
    Utility class for document processing that interfaces with NewsScheduler.

    This utility provides a simple interface for the API routes to interact with
    the NewsScheduler's document processing functionality without exposing the
    underlying scheduler implementation details.
    """

    def __init__(self):
        """Initialize the document scheduler utility."""
        logger.debug("Document scheduler utility initialized")

    def get_status(self, username: str) -> Dict[str, Any]:
        """
        Get document processing status for a user.

        Args:
            username: The username to get status for

        Returns:
            Dictionary containing document processing status information
        """
        try:
            scheduler = get_news_scheduler()
            return scheduler.get_document_scheduler_status(username)

        except Exception:
            logger.exception(
                f"Error getting document scheduler status for {username}"
            )
            return {
                "error": "Failed to get scheduler status",
                "is_running": False,
                "last_run_time": None,
                "next_run_time": None,
                "total_processed": 0,
                "currently_processing": 0,
                "processing_ids": [],
                "settings": {},
            }

    def trigger_manual_run(self, username: str) -> tuple[bool, str]:
        """
        Trigger a manual document processing run for a user.

        Args:
            username: The username to trigger processing for

        Returns:
            Tuple of (success: bool, message: str)
        """
        try:
            scheduler = get_news_scheduler()
            success = scheduler.trigger_document_processing(username)

            if success:
                return True, "Manual document processing triggered successfully"
            else:
                return (
                    False,
                    "Failed to trigger document processing - user may not be active or processing disabled",
                )

        except Exception:
            logger.exception(
                f"Error triggering manual document processing for {username}"
            )
            return False, "Failed to trigger manual processing"


# Singleton instance getter
_scheduler_util_instance = None


def get_document_scheduler() -> DocumentSchedulerUtil:
    """
    Get the singleton document scheduler utility instance.

    Note: This returns the utility class that interfaces with NewsScheduler,
    not a standalone scheduler instance.
    """
    global _scheduler_util_instance
    if _scheduler_util_instance is None:
        _scheduler_util_instance = DocumentSchedulerUtil()
    return _scheduler_util_instance
