"""
Edge case tests for text input limits and validation.

Tests boundary conditions for:
- Maximum text length (50K chars)
- Empty and whitespace-only text
- Unicode edge cases (mixed scripts, RTL, emojis)
- Special characters and control characters
"""

import pytest


class TestMaxTextLength:
    """Test maximum text length handling."""
    
    def test_text_at_max_limit(self):
        """Text exactly at 50K limit should be accepted."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "A" * 50000
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert len(req.text) == 50000
    
    def test_text_one_over_max_rejected(self):
        """Text one char over 50K should be rejected."""
        from veena3modal.api.schemas import TTSGenerateRequest
        from pydantic import ValidationError
        
        text = "A" * 50001
        with pytest.raises(ValidationError) as exc_info:
            TTSGenerateRequest(text=text, speaker="lipakshi")
        
        assert "50000" in str(exc_info.value).lower() or "max" in str(exc_info.value).lower()
    
    def test_text_way_over_max_rejected(self):
        """Very long text (100K) should be rejected."""
        from veena3modal.api.schemas import TTSGenerateRequest
        from pydantic import ValidationError
        
        text = "A" * 100000
        with pytest.raises(ValidationError):
            TTSGenerateRequest(text=text, speaker="lipakshi")
    
    def test_unicode_text_at_max(self):
        """Unicode text at 50K chars (not bytes) should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        # Hindi text - each char is 3 bytes in UTF-8
        text = "क" * 50000  # 50K chars, ~150K bytes
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert len(req.text) == 50000


class TestEmptyAndWhitespace:
    """Test empty and whitespace-only text handling."""
    
    def test_empty_text_rejected(self):
        """Empty text should be rejected."""
        from veena3modal.api.schemas import TTSGenerateRequest
        from pydantic import ValidationError
        
        with pytest.raises(ValidationError):
            TTSGenerateRequest(text="", speaker="lipakshi")
    
    def test_whitespace_only_rejected(self):
        """Whitespace-only text should be rejected."""
        from veena3modal.api.schemas import TTSGenerateRequest
        from pydantic import ValidationError
        
        with pytest.raises(ValidationError):
            TTSGenerateRequest(text="   \t\n  ", speaker="lipakshi")
    
    def test_text_with_leading_trailing_whitespace(self):
        """Text with leading/trailing whitespace should be accepted (stripped)."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        req = TTSGenerateRequest(text="  Hello world  ", speaker="lipakshi")
        # Should work - the validation passes if there's non-whitespace content
        assert "Hello world" in req.text or req.text.strip() == "Hello world"


class TestUnicodeEdgeCases:
    """Test Unicode edge cases."""
    
    def test_mixed_scripts(self):
        """Mixed scripts (English + Hindi + Telugu) should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Hello नमस्ते హలో"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert req.text == text
    
    def test_emojis_accepted(self):
        """Emojis should be accepted in text."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Hello! 😀🎉🔥 How are you?"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert "😀" in req.text
    
    def test_rtl_text(self):
        """Right-to-left text (Arabic) should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "مرحبا بالعالم"  # "Hello World" in Arabic
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert req.text == text
    
    def test_mixed_rtl_ltr(self):
        """Mixed RTL and LTR text should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Hello مرحبا World"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert req.text == text
    
    def test_zero_width_characters(self):
        """Zero-width characters should be handled."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        # Zero-width joiner and non-joiner
        text = "Hello\u200B\u200CWorld"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        # Should accept (normalizer may clean these up)
        assert req.text is not None
    
    def test_combining_characters(self):
        """Combining diacritical marks should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        # e with combining acute accent
        text = "cafe\u0301"  # café
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert req.text is not None
    
    def test_surrogate_pairs(self):
        """Characters outside BMP (emoji, rare chars) should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        # Mathematical bold A (outside BMP)
        text = "Test 𝐀𝐁𝐂"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert "𝐀" in req.text


class TestSpecialCharacters:
    """Test special characters handling."""
    
    def test_newlines_accepted(self):
        """Newlines should be accepted in text."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Line 1\nLine 2\nLine 3"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert "\n" in req.text or req.text.count("Line") == 3
    
    def test_tabs_accepted(self):
        """Tabs should be accepted."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Column1\tColumn2\tColumn3"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert req.text is not None
    
    def test_quotes_and_apostrophes(self):
        """Various quote styles should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = 'He said "Hello" and \'Goodbye\' with "curly quotes"'
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert req.text is not None
    
    def test_html_entities_literal(self):
        """HTML-like text should be treated literally."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Use &lt; for less than and &gt; for greater than"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert "&lt;" in req.text
    
    def test_urls_in_text(self):
        """URLs in text should be accepted."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Visit https://example.com/path?query=value for more info"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert "https://" in req.text
    
    def test_email_in_text(self):
        """Email addresses in text should be accepted."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Contact us at support@example.com for help"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert "@" in req.text


class TestEmotionTagEdgeCases:
    """Test emotion tag edge cases."""
    
    def test_multiple_emotion_tags(self):
        """Multiple emotion tags in text should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "[happy] Hello! [sad] Goodbye. [angry] Stop!"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert req.text is not None
    
    def test_emotion_tag_at_end(self):
        """Emotion tag at end of text should work."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "Hello world [excited]"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        assert "[excited]" in req.text.lower() or "excited" in req.text.lower()
    
    def test_legacy_angle_bracket_emotion(self):
        """Legacy <emotion> tags should be normalized to [emotion]."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "<happy> Hello there!"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        normalized = req.get_normalized_text()
        # Should be converted to bracket format
        assert "[happy]" in normalized.lower() or "<happy>" not in normalized
    
    def test_unknown_emotion_tag(self):
        """Unknown emotion tag should be preserved as-is."""
        from veena3modal.api.schemas import TTSGenerateRequest
        
        text = "[unknown_emotion] Hello!"
        req = TTSGenerateRequest(text=text, speaker="lipakshi")
        # Should not crash
        assert req.text is not None

