"""
Unit tests for translation API
"""
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, AsyncMock

# Import the FastAPI app
from main import app


class TestAPITranslate:
    """Tests for translation endpoint"""

    def test_translate_missing_text(self):
        """Test translation without text parameter"""
        client = TestClient(app)
        response = client.post(
            "/api/translate",
            json={}
        )
        assert response.status_code == 400  # Bad request

    def test_translate_with_text(self):
        """Test translation with text parameter"""
        client = TestClient(app)

        # Mock httpx to avoid actual API calls
        with patch('httpx.AsyncClient') as mock_client_class:
            mock_client = AsyncMock()
            mock_response = AsyncMock()
            mock_response.status_code = 200
            mock_response.json.return_value = [
                [["Bonjour le monde", "Hello world", "", 1]],
                ["en", "fr"],
                None,
                None,
            ]
            mock_client.get.return_value = mock_response
            mock_client_class.return_value.__aenter__.return_value = mock_client

            response = client.post(
                "/api/translate",
                json={"text": "Hello world"}
            )

            # Should succeed (may fail with actual API, but we're mocking)
            assert response.status_code in [200, 500]

    def test_translate_long_text(self):
        """Test translation with text longer than 5000 chars"""
        client = TestClient(app)

        long_text = "Hello " * 2000  # > 5000 chars

        with patch('httpx.AsyncClient') as mock_client_class:
            mock_client = AsyncMock()
            mock_response = AsyncMock()
            mock_response.status_code = 200
            mock_response.json.return_value = [
                [["Translated text"]],
                ["en", "fr"],
                None,
                None,
            ]
            mock_client.get.return_value = mock_response
            mock_client_class.return_value.__aenter__.return_value = mock_client

            response = client.post(
                "/api/translate",
                json={"text": long_text}
            )

            # Should truncate to 5000 chars
            assert response.status_code in [200, 500]

    def test_translate_empty_text(self):
        """Test translation with empty text"""
        client = TestClient(app)
        response = client.post(
            "/api/translate",
            json={"text": ""}
        )

        # Should handle empty text gracefully
        assert response.status_code in [200, 400, 500]

    def test_translate_special_characters(self):
        """Test translation with special characters"""
        client = TestClient(app)

        special_text = "Hello! @#$%^&*()_+ World"

        with patch('httpx.AsyncClient') as mock_client_class:
            mock_client = AsyncMock()
            mock_response = AsyncMock()
            mock_response.status_code = 200
            mock_response.json.return_value = [
                [[special_text]],
                ["en", "fr"],
                None,
                None,
            ]
            mock_client.get.return_value = mock_response
            mock_client_class.return_value.__aenter__.return_value = mock_client

            response = client.post(
                "/api/translate",
                json={"text": special_text}
            )

            assert response.status_code in [200, 500]

    def test_translate_unicode_text(self):
        """Test translation with unicode characters"""
        client = TestClient(app)

        unicode_text = "Hello 世界 🌍"

        with patch('httpx.AsyncClient') as mock_client_class:
            mock_client = AsyncMock()
            mock_response = AsyncMock()
            mock_response.status_code = 200
            mock_response.json.return_value = [
                [[unicode_text]],
                ["en", "fr"],
                None,
                None,
            ]
            mock_client.get.return_value = mock_response
            mock_client_class.return_value.__aenter__.return_value = mock_client

            response = client.post(
                "/api/translate",
                json={"text": unicode_text}
            )

            assert response.status_code in [200, 500]


class TestAPIAnimeSeasons:
    """Tests for anime seasons endpoint"""

    def test_anime_seasons_missing_url(self):
        """Test seasons endpoint without URL parameter"""
        client = TestClient(app)
        response = client.get("/api/anime/seasons")
        assert response.status_code == 422  # Validation error

    def test_anime_seasons_with_url(self):
        """Test seasons endpoint with URL parameter"""
        client = TestClient(app)
        response = client.get(
            "/api/anime/seasons?url=https://anime-sama.si/catalogue/test/vostfr/"
        )

        # May return 200 with seasons or 200 with empty list
        # Could also return errors if the site is down
        assert response.status_code in [200, 404, 500]

        if response.status_code == 200:
            data = response.json()
            assert "seasons" in data
            assert isinstance(data["seasons"], list)

    def test_anime_seasons_non_anime_sama(self):
        """Test seasons endpoint with non-AnimeSama URL"""
        client = TestClient(app)
        response = client.get(
            "/api/anime/seasons?url=https://neko-sama.fr/anime/test"
        )

        # Should return 200 with empty seasons list
        assert response.status_code == 200
        data = response.json()
        assert "seasons" in data
        assert data["seasons"] == []
        assert "message" in data
