"""
Tests for NotificationService edge cases: test_service() error paths
and get_service_type() pattern coverage.
"""

from unittest.mock import patch, MagicMock

from local_deep_research.notifications.service import NotificationService


class TestTestServiceErrorPaths:
    """Tests for test_service() error branches."""

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    @patch(
        "local_deep_research.notifications.service.NotificationURLValidator.validate_service_url",
        return_value=(False, "blocked by SSRF check"),
    )
    def test_ssrf_validation_fails(self, mock_validator, mock_apprise_class):
        service = NotificationService(outbound_allowed=True)
        result = service.test_service("http://169.254.169.254/metadata")
        assert result["success"] is False
        assert "Invalid" in result["error"]

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    @patch(
        "local_deep_research.notifications.service.NotificationURLValidator.validate_service_url",
        return_value=(True, None),
    )
    def test_apprise_add_fails(self, mock_validator, mock_apprise_class):
        mock_instance = MagicMock()
        mock_instance.add.return_value = False
        mock_apprise_class.return_value = mock_instance

        service = NotificationService(outbound_allowed=True)
        result = service.test_service("discord://webhook/token")
        assert result["success"] is False
        assert "Failed to add" in result["error"]

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    @patch(
        "local_deep_research.notifications.service.NotificationURLValidator.validate_service_url",
        return_value=(True, None),
    )
    def test_apprise_notify_fails(self, mock_validator, mock_apprise_class):
        mock_instance = MagicMock()
        mock_instance.add.return_value = True
        mock_instance.notify.return_value = False
        mock_apprise_class.return_value = mock_instance

        service = NotificationService(outbound_allowed=True)
        result = service.test_service("discord://webhook/token")
        assert result["success"] is False
        assert "Failed to send" in result["error"]

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    @patch(
        "local_deep_research.notifications.service.NotificationURLValidator.validate_service_url",
        return_value=(True, None),
    )
    def test_success_path(self, mock_validator, mock_apprise_class):
        mock_instance = MagicMock()
        mock_instance.add.return_value = True
        mock_instance.notify.return_value = True
        mock_apprise_class.return_value = mock_instance

        service = NotificationService(outbound_allowed=True)
        result = service.test_service("discord://webhook/token")
        assert result["success"] is True
        assert "successfully" in result["message"]

    @patch("local_deep_research.notifications.service.apprise.Apprise")
    @patch(
        "local_deep_research.notifications.service.NotificationURLValidator.validate_service_url",
        return_value=(False, "internal DNS resolution error"),
    )
    def test_validation_error_message_not_leaked(
        self, mock_validator, mock_apprise_class
    ):
        service = NotificationService(outbound_allowed=True)
        result = service.test_service("http://evil.com")
        assert result["success"] is False
        # Internal error detail should NOT appear in the user-facing message
        assert "internal DNS" not in result["error"]
        assert "Invalid" in result["error"]


class TestGetServiceTypePatterns:
    """Tests for get_service_type() across all SERVICE_PATTERNS."""

    def test_email_detection(self):
        service = NotificationService(outbound_allowed=True)
        assert service.get_service_type("mailto://user@example.com") == "email"

    def test_slack_detection(self):
        service = NotificationService(outbound_allowed=True)
        assert (
            service.get_service_type("slack://token_a/token_b/token_c")
            == "slack"
        )

    def test_telegram_detection(self):
        service = NotificationService(outbound_allowed=True)
        assert (
            service.get_service_type("tgram://bottoken/chat_id") == "telegram"
        )

    def test_smtp_detection(self):
        service = NotificationService(outbound_allowed=True)
        assert service.get_service_type("smtp://user:pass@mail.com") == "smtp"

    def test_smtps_detection(self):
        service = NotificationService(outbound_allowed=True)
        assert service.get_service_type("smtps://user:pass@mail.com") == "smtp"
