"""Tests for document_scheduler module."""

from unittest.mock import MagicMock, patch


from local_deep_research.research_scheduler.document_scheduler import (
    DocumentSchedulerUtil,
    get_document_scheduler,
)


class TestDocumentSchedulerUtilInit:
    """Tests for DocumentSchedulerUtil initialization."""

    def test_initializes_successfully(self):
        """Should initialize without error."""
        scheduler = DocumentSchedulerUtil()
        assert scheduler is not None


class TestGetStatus:
    """Tests for get_status method."""

    def test_returns_status_from_news_scheduler(
        self, mock_news_scheduler, sample_status
    ):
        """Should return status from news scheduler."""
        scheduler = DocumentSchedulerUtil()

        with patch(
            "local_deep_research.research_scheduler.document_scheduler.get_news_scheduler",
            return_value=mock_news_scheduler,
        ):
            result = scheduler.get_status("testuser")

        mock_news_scheduler.get_document_scheduler_status.assert_called_once_with(
            "testuser"
        )
        assert result["is_running"] is True
        assert result["total_processed"] == 100

    def test_returns_error_status_on_exception(self):
        """Should return error status on exception."""
        scheduler = DocumentSchedulerUtil()

        with patch(
            "local_deep_research.research_scheduler.document_scheduler.get_news_scheduler",
            side_effect=Exception("Connection error"),
        ):
            result = scheduler.get_status("testuser")

        assert result["error"] == "Failed to get scheduler status"
        assert result["is_running"] is False
        assert result["total_processed"] == 0


class TestTriggerManualRun:
    """Tests for trigger_manual_run method."""

    def test_returns_success_when_trigger_succeeds(self, mock_news_scheduler):
        """Should return success when trigger succeeds."""
        scheduler = DocumentSchedulerUtil()

        with patch(
            "local_deep_research.research_scheduler.document_scheduler.get_news_scheduler",
            return_value=mock_news_scheduler,
        ):
            success, message = scheduler.trigger_manual_run("testuser")

        assert success is True
        assert "successfully" in message
        mock_news_scheduler.trigger_document_processing.assert_called_once_with(
            "testuser"
        )

    def test_returns_failure_when_trigger_fails(self):
        """Should return failure when trigger fails."""
        scheduler = DocumentSchedulerUtil()
        mock_scheduler = MagicMock()
        mock_scheduler.trigger_document_processing.return_value = False

        with patch(
            "local_deep_research.research_scheduler.document_scheduler.get_news_scheduler",
            return_value=mock_scheduler,
        ):
            success, message = scheduler.trigger_manual_run("testuser")

        assert success is False
        assert "Failed" in message or "disabled" in message

    def test_returns_failure_on_exception(self):
        """Should return failure on exception."""
        scheduler = DocumentSchedulerUtil()

        with patch(
            "local_deep_research.research_scheduler.document_scheduler.get_news_scheduler",
            side_effect=Exception("Connection error"),
        ):
            success, message = scheduler.trigger_manual_run("testuser")

        assert success is False
        assert "Failed" in message


class TestGetDocumentScheduler:
    """Tests for get_document_scheduler function."""

    def test_returns_singleton_instance(self):
        """Should return singleton instance."""
        # Reset singleton
        import local_deep_research.research_scheduler.document_scheduler as mod

        mod._scheduler_util_instance = None

        instance1 = get_document_scheduler()
        instance2 = get_document_scheduler()

        assert instance1 is instance2

    def test_creates_new_instance_when_none(self):
        """Should create new instance when none exists."""
        import local_deep_research.research_scheduler.document_scheduler as mod

        mod._scheduler_util_instance = None

        instance = get_document_scheduler()

        assert isinstance(instance, DocumentSchedulerUtil)

    def test_returns_existing_instance(self):
        """Should return existing instance."""
        existing = DocumentSchedulerUtil()
        import local_deep_research.research_scheduler.document_scheduler as mod

        mod._scheduler_util_instance = existing

        instance = get_document_scheduler()

        assert instance is existing
