"""
Tests for notification URL builder.
"""

import pytest
from unittest.mock import Mock

from local_deep_research.notifications.url_builder import build_notification_url
from local_deep_research.security.url_validator import (
    URLValidator,
    URLValidationError,
)


class TestValidateURL:
    """Tests for URL validation function."""

    def test_valid_http_url(self):
        """Test validation accepts valid HTTP URLs."""
        assert URLValidator.validate_http_url("http://localhost:5000/path")
        assert URLValidator.validate_http_url("http://example.com/research/123")
        assert URLValidator.validate_http_url("http://192.168.1.100:8080/test")

    def test_valid_https_url(self):
        """Test validation accepts valid HTTPS URLs."""
        assert URLValidator.validate_http_url("https://example.com/path")
        assert URLValidator.validate_http_url(
            "https://ldr.example.com:8443/research"
        )
        assert URLValidator.validate_http_url(
            "https://sub.domain.example.com/test"
        )

    def test_empty_url(self):
        """Test validation rejects empty URLs."""
        with pytest.raises(URLValidationError, match="non-empty string"):
            URLValidator.validate_http_url("")

    def test_none_url(self):
        """Test validation rejects None."""
        with pytest.raises(URLValidationError, match="non-empty string"):
            URLValidator.validate_http_url(None)

    def test_invalid_type(self):
        """Test validation rejects non-string types."""
        with pytest.raises(URLValidationError, match="non-empty string"):
            URLValidator.validate_http_url(123)

    def test_missing_scheme(self):
        """Test validation rejects URLs without scheme."""
        with pytest.raises(URLValidationError, match="must have a scheme"):
            URLValidator.validate_http_url("example.com/path")

    def test_invalid_scheme(self):
        """Test validation rejects non-http(s) schemes."""
        with pytest.raises(URLValidationError, match="must be http or https"):
            URLValidator.validate_http_url("ftp://example.com/path")

        with pytest.raises(URLValidationError, match="must be http or https"):
            URLValidator.validate_http_url("javascript://alert(1)")

        with pytest.raises(URLValidationError, match="must be http or https"):
            URLValidator.validate_http_url("file:///etc/passwd")

    def test_missing_hostname(self):
        """Test validation rejects URLs without hostname."""
        with pytest.raises(URLValidationError, match="must have a hostname"):
            URLValidator.validate_http_url("http:///path")

    def test_invalid_hostname(self):
        """Test validation rejects malformed hostnames."""
        with pytest.raises(URLValidationError, match="Invalid hostname"):
            URLValidator.validate_http_url("http://.example.com/path")

        with pytest.raises(URLValidationError, match="Invalid hostname"):
            URLValidator.validate_http_url("http://example.com./path")

    def test_url_with_port(self):
        """Test validation accepts URLs with ports."""
        assert URLValidator.validate_http_url("http://localhost:5000/path")
        assert URLValidator.validate_http_url("https://example.com:8443/path")

    def test_url_with_query_params(self):
        """Test validation accepts URLs with query parameters."""
        assert URLValidator.validate_http_url(
            "http://example.com/path?param=value"
        )
        assert URLValidator.validate_http_url(
            "https://example.com/research?id=123&user=test"
        )

    def test_url_with_fragment(self):
        """Test validation accepts URLs with fragments."""
        assert URLValidator.validate_http_url("http://example.com/path#section")
        assert URLValidator.validate_http_url(
            "https://example.com/research/123#results"
        )


class TestBuildNotificationURL:
    """Tests for build_notification_url function."""

    def test_build_with_fallback_only(self):
        """Test URL construction with fallback only."""
        url = build_notification_url("/research/123")
        assert url == "http://localhost:5000/research/123"

    def test_build_with_custom_fallback(self):
        """Test URL construction with custom fallback."""
        url = build_notification_url(
            "/research/123", fallback_base="https://example.com"
        )
        assert url == "https://example.com/research/123"

    def test_build_with_external_url_setting(self):
        """Test URL construction with external_url setting."""
        mock_settings = Mock()
        # Mock returns different values for different settings
        mock_settings.get_setting.side_effect = lambda key, default=None: {
            "app.external_url": "https://ldr.example.com",
            "app.host": "localhost",
            "app.port": 5000,
        }.get(key, default)

        url = build_notification_url(
            "/research/123", settings_manager=mock_settings
        )

        assert url == "https://ldr.example.com/research/123"
        # Verify external_url was requested
        mock_settings.get_setting.assert_any_call(
            "app.external_url", default=""
        )

    def test_build_with_external_url_trailing_slash(self):
        """Test URL construction strips trailing slash from base."""
        mock_settings = Mock()
        mock_settings.get_setting.return_value = "https://ldr.example.com/"

        url = build_notification_url(
            "/research/123", settings_manager=mock_settings
        )

        assert url == "https://ldr.example.com/research/123"

    def test_build_with_host_port_fallback(self):
        """Test URL construction from app.host and app.port."""
        mock_settings = Mock()
        # external_url is empty, should fall back to host/port
        mock_settings.get_setting.side_effect = lambda key, default=None: {
            "app.external_url": "",
            "app.host": "192.168.1.100",
            "app.port": 8080,
        }.get(key, default)

        url = build_notification_url(
            "/research/123", settings_manager=mock_settings
        )

        assert url == "http://192.168.1.100:8080/research/123"

    def test_build_converts_bind_all_to_localhost(self):
        """Test 0.0.0.0 is converted to localhost in URLs."""
        mock_settings = Mock()
        mock_settings.get_setting.side_effect = lambda key, default=None: {
            "app.external_url": "",
            "app.host": "0.0.0.0",
            "app.port": 5000,
        }.get(key, default)

        url = build_notification_url(
            "/research/123", settings_manager=mock_settings
        )

        assert url == "http://localhost:5000/research/123"

    def test_build_converts_ipv6_bind_all_to_localhost(self):
        """Test :: is converted to localhost in URLs."""
        mock_settings = Mock()
        mock_settings.get_setting.side_effect = lambda key, default=None: {
            "app.external_url": "",
            "app.host": "::",
            "app.port": 5000,
        }.get(key, default)

        url = build_notification_url(
            "/research/123", settings_manager=mock_settings
        )

        assert url == "http://localhost:5000/research/123"

    def test_build_adds_leading_slash_to_path(self):
        """Test path without leading slash gets one added."""
        url = build_notification_url("research/123")
        assert url == "http://localhost:5000/research/123"

    def test_build_with_complex_path(self):
        """Test URL construction with complex paths."""
        url = build_notification_url(
            "/research/123/results?format=json#section"
        )
        assert (
            url
            == "http://localhost:5000/research/123/results?format=json#section"
        )

    def test_build_validation_enabled_by_default(self):
        """Test validation is enabled by default."""
        mock_settings = Mock()
        mock_settings.get_setting.return_value = "not-a-valid-url"

        with pytest.raises(URLValidationError):
            build_notification_url("/path", settings_manager=mock_settings)

    def test_build_validation_can_be_disabled(self):
        """Test validation can be disabled."""
        mock_settings = Mock()
        mock_settings.get_setting.return_value = "not-a-valid-url"

        # Should not raise when validation disabled
        url = build_notification_url(
            "/path", settings_manager=mock_settings, validate=False
        )
        assert url == "not-a-valid-url/path"

    def test_build_handles_settings_exception(self):
        """Test that settings errors are wrapped in URLValidationError."""
        mock_settings = Mock()
        mock_settings.get_setting.side_effect = Exception("Database error")

        # Should raise URLValidationError wrapping the original exception
        with pytest.raises(
            URLValidationError, match="Failed to build notification URL"
        ):
            build_notification_url(
                "/research/123", settings_manager=mock_settings
            )

    def test_build_with_port_in_external_url(self):
        """Test external_url with port number."""
        mock_settings = Mock()
        mock_settings.get_setting.return_value = "https://ldr.example.com:8443"

        url = build_notification_url(
            "/research/123", settings_manager=mock_settings
        )

        assert url == "https://ldr.example.com:8443/research/123"

    def test_build_invalid_url_raises_error(self):
        """Test that invalid URL construction raises URLValidationError."""
        mock_settings = Mock()
        # This will create an invalid URL (no scheme)
        mock_settings.get_setting.return_value = "example.com"

        with pytest.raises(
            URLValidationError,
            match="Constructed invalid URL|must have a scheme",
        ):
            build_notification_url("/path", settings_manager=mock_settings)


class TestIntegration:
    """Integration tests for URL builder."""

    def test_realistic_local_development_scenario(self):
        """Test typical local development setup."""
        url = build_notification_url("/research/abc123")
        assert url == "http://localhost:5000/research/abc123"
        assert URLValidator.validate_http_url(url)

    def test_realistic_production_scenario(self):
        """Test typical production setup with external URL."""
        mock_settings = Mock()
        mock_settings.get_setting.return_value = "https://ldr.company.com"

        url = build_notification_url(
            "/research/abc123", settings_manager=mock_settings
        )

        assert url == "https://ldr.company.com/research/abc123"
        assert URLValidator.validate_http_url(url)

    def test_realistic_custom_port_scenario(self):
        """Test setup with custom port."""
        mock_settings = Mock()
        mock_settings.get_setting.side_effect = lambda key, default=None: {
            "app.external_url": "",
            "app.host": "server.local",
            "app.port": 8080,
        }.get(key, default)

        url = build_notification_url(
            "/research/abc123", settings_manager=mock_settings
        )

        assert url == "http://server.local:8080/research/abc123"
        assert URLValidator.validate_http_url(url)
