import uuid
from unittest.mock import patch

from src.broadie.utils import slugify


class TestSlugify:
    """Unit tests for the slugify utility function."""

    def test_slugify_basic_string(self):
        """Test slugification of a basic string."""
        result = slugify("Hello World")
        assert result == "hello-world"

    def test_slugify_with_special_characters(self):
        """Test slugification with special characters."""
        result = slugify("Hello, World!")
        assert result == "hello-world"

    def test_slugify_with_numbers(self):
        """Test slugification preserving numbers."""
        result = slugify("Test123 Agent")
        assert result == "test123-agent"

    def test_slugify_with_multiple_spaces(self):
        """Test slugification with multiple consecutive spaces."""
        result = slugify("Hello    World")
        assert result == "hello-world"

    def test_slugify_with_leading_trailing_spaces(self):
        """Test slugification with leading and trailing spaces."""
        result = slugify("  Hello World  ")
        assert result == "hello-world"

    def test_slugify_with_hyphens(self):
        """Test slugification with existing hyphens."""
        result = slugify("hello-world")
        assert result == "hello-world"

    def test_slugify_with_underscores(self):
        """Test slugification converting underscores to hyphens."""
        result = slugify("hello_world_test")
        assert result == "hello-world-test"

    def test_slugify_mixed_case_with_symbols(self):
        """Test slugification with mixed case and various symbols."""
        result = slugify("My@Agent#2024!")
        assert result == "my-agent-2024"

    def test_slugify_only_special_characters(self):
        """Test slugification with only special characters returns UUID."""
        with patch("uuid.uuid4") as mock_uuid:
            mock_uuid.return_value = uuid.UUID("12345678-1234-5678-9abc-123456789012")
            result = slugify("@#$%^&*()")
            assert result == "12345678-1234-5678-9abc-123456789012"

    def test_slugify_empty_string(self):
        """Test slugification of empty string returns UUID."""
        with patch("uuid.uuid4") as mock_uuid:
            mock_uuid.return_value = uuid.UUID("12345678-1234-5678-9abc-123456789012")
            result = slugify("")
            assert result == "12345678-1234-5678-9abc-123456789012"

    def test_slugify_only_whitespace(self):
        """Test slugification of only whitespace returns UUID."""
        with patch("uuid.uuid4") as mock_uuid:
            mock_uuid.return_value = uuid.UUID("12345678-1234-5678-9abc-123456789012")
            result = slugify("   ")
            assert result == "12345678-1234-5678-9abc-123456789012"

    def test_slugify_consecutive_special_chars(self):
        """Test slugification with consecutive special characters."""
        result = slugify("hello!!!world???test")
        assert result == "hello-world-test"

    def test_slugify_starts_ends_with_hyphens(self):
        """Test slugification removes leading and trailing hyphens."""
        result = slugify("-hello-world-")
        assert result == "hello-world"

    def test_slugify_unicode_characters(self):
        """Test slugification with unicode characters."""
        result = slugify("café résumé naïve")
        assert result == "caf-r-sum-na-ve"

    def test_slugify_very_long_string(self):
        """Test slugification with a very long string."""
        long_string = "This is a very long string with many words " * 10
        result = slugify(long_string)
        # Should still work and contain expected pattern
        assert "this-is-a-very-long-string-with-many-words" in result
        assert result.startswith("this-is-a-very-long-string")

    def test_slugify_alphanumeric_only(self):
        """Test slugification with alphanumeric characters only."""
        result = slugify("HelloWorld123")
        assert result == "helloworld123"

    def test_slugify_with_dots_and_slashes(self):
        """Test slugification with file-like patterns."""
        result = slugify("config/settings.py")
        assert result == "config-settings-py"

    def test_slugify_with_email_pattern(self):
        """Test slugification with email-like patterns."""
        result = slugify("user@example.com")
        assert result == "user-example-com"

    def test_slugify_preserves_case_sensitivity_false(self):
        """Test that slugify converts to lowercase."""
        result = slugify("UPPERCASE")
        assert result == "uppercase"

    def test_slugify_with_tab_characters(self):
        """Test slugification with tab characters."""
        result = slugify("hello\tworld")
        assert result == "hello-world"

    def test_slugify_with_newline_characters(self):
        """Test slugification with newline characters."""
        result = slugify("hello\nworld")
        assert result == "hello-world"

    def test_slugify_regex_edge_cases(self):
        """Test slugification with regex edge cases."""
        # Test with characters that might be problematic for regex
        result = slugify("hello[world]")
        assert result == "hello-world"

        result = slugify("hello{world}")
        assert result == "hello-world"

        result = slugify("hello(world)")
        assert result == "hello-world"

    def test_slugify_fallback_uuid_format(self):
        """Test that fallback UUID is properly formatted."""
        with patch("uuid.uuid4") as mock_uuid:
            test_uuid = uuid.UUID("12345678-1234-5678-9abc-123456789012")
            mock_uuid.return_value = test_uuid

            result = slugify("!!!!")

            # Verify it's a valid UUID string
            assert result == str(test_uuid)
            assert len(result) == 36  # Standard UUID length with hyphens
            mock_uuid.assert_called_once()
