"""Tests for Veo-specific tools.

Generated by GRIMLOCK MCP Factory
"""

import json

import httpx
import pytest
import respx

from videogen_mcp.tools.generate_creative_video import generate_creative_video
from videogen_mcp.tools.generate_video_from_image import generate_video_from_image


class TestGenerateCreativeVideo:
    """Test cases for generate_creative_video tool."""

    @pytest.mark.asyncio
    async def test_generate_creative_video_success(
        self, mock_env, mock_httpx_client, sample_veo_generate_response
    ):
        """Should generate creative video successfully."""
        mock_httpx_client.post(
            "https://generativelanguage.googleapis.com/v1beta/models/veo-3.1-generate-preview:predictLongRunning"
        ).mock(
            return_value=httpx.Response(200, json=sample_veo_generate_response)
        )

        result = await generate_creative_video({
            "prompt": "A cinematic aerial shot of mountains at sunset",
        })
        data = json.loads(result[0].text)

        assert data["success"] is True
        assert data["job_id"] == "operations/veo-generate-abc123xyz789"
        assert data["service"] == "veo"
        assert data["status"] == "pending"

    @pytest.mark.asyncio
    async def test_generate_creative_video_with_options(
        self, mock_env, mock_httpx_client, sample_veo_generate_response
    ):
        """Should generate creative video with all options."""
        mock_httpx_client.post(
            "https://generativelanguage.googleapis.com/v1beta/models/veo-2.0-generate-exp:predictLongRunning"
        ).mock(
            return_value=httpx.Response(200, json=sample_veo_generate_response)
        )

        result = await generate_creative_video({
            "prompt": "Abstract flowing particles in neon colors",
            "negative_prompt": "blurry, low quality",
            "aspect_ratio": "9:16",
            "duration": 6,
            "model": "veo-2.0-generate-exp",
            "seed": 12345,
            "sample_count": 2,
        })
        data = json.loads(result[0].text)

        assert data["success"] is True
        assert data["model_used"] == "veo-2.0-generate-exp"

    @pytest.mark.asyncio
    async def test_generate_creative_video_content_blocked(self, mock_env, mock_httpx_client):
        """Should handle content blocked error."""
        mock_httpx_client.post(
            "https://generativelanguage.googleapis.com/v1beta/models/veo-3.1-generate-preview:predictLongRunning"
        ).mock(
            return_value=httpx.Response(400, json={"error": {"message": "Content blocked by safety filter"}})
        )

        result = await generate_creative_video({
            "prompt": "Inappropriate content",
        })
        data = json.loads(result[0].text)

        assert data["error"] is True

    @pytest.mark.asyncio
    async def test_generate_creative_video_quota_exceeded(self, mock_env, mock_httpx_client):
        """Should handle quota exceeded error."""
        mock_httpx_client.post(
            "https://generativelanguage.googleapis.com/v1beta/models/veo-3.1-generate-preview:predictLongRunning"
        ).mock(
            return_value=httpx.Response(429, json={"error": {"message": "Quota exceeded"}})
        )

        result = await generate_creative_video({
            "prompt": "A beautiful sunset",
        })
        data = json.loads(result[0].text)

        assert data["error"] is True


class TestGenerateVideoFromImage:
    """Test cases for generate_video_from_image tool."""

    @pytest.mark.asyncio
    async def test_generate_video_from_image_success(
        self, mock_env, mock_httpx_client, sample_veo_generate_response, sample_image_bytes
    ):
        """Should generate video from image successfully."""
        # Mock image fetch
        mock_httpx_client.get("https://example.com/test-image.png").mock(
            return_value=httpx.Response(
                200,
                content=sample_image_bytes,
                headers={"content-type": "image/png"},
            )
        )

        # Mock Veo API
        mock_httpx_client.post(
            "https://generativelanguage.googleapis.com/v1beta/models/veo-3.1-generate-preview:predictLongRunning"
        ).mock(
            return_value=httpx.Response(200, json=sample_veo_generate_response)
        )

        result = await generate_video_from_image({
            "image_url": "https://example.com/test-image.png",
            "prompt": "The image slowly zooms in with subtle movement",
        })
        data = json.loads(result[0].text)

        assert data["success"] is True
        assert data["job_id"] == "operations/veo-generate-abc123xyz789"
        assert data["service"] == "veo"

    @pytest.mark.asyncio
    async def test_generate_video_from_image_with_options(
        self, mock_env, mock_httpx_client, sample_veo_generate_response, sample_image_bytes
    ):
        """Should generate video from image with all options."""
        mock_httpx_client.get("https://example.com/photo.jpg").mock(
            return_value=httpx.Response(
                200,
                content=sample_image_bytes,
                headers={"content-type": "image/jpeg"},
            )
        )

        mock_httpx_client.post(
            "https://generativelanguage.googleapis.com/v1beta/models/veo-3.1-generate-preview:predictLongRunning"
        ).mock(
            return_value=httpx.Response(200, json=sample_veo_generate_response)
        )

        result = await generate_video_from_image({
            "image_url": "https://example.com/photo.jpg",
            "prompt": "Camera slowly pans across the scene",
            "negative_prompt": "sudden movements, jitter",
            "duration": 4,
        })
        data = json.loads(result[0].text)

        assert data["success"] is True

    @pytest.mark.asyncio
    async def test_generate_video_from_image_not_found(self, mock_env, mock_httpx_client):
        """Should handle image not found error."""
        mock_httpx_client.get("https://example.com/missing.jpg").mock(
            return_value=httpx.Response(404, text="Not found")
        )

        result = await generate_video_from_image({
            "image_url": "https://example.com/missing.jpg",
            "prompt": "Animate this image",
        })
        data = json.loads(result[0].text)

        assert data["error"] is True
        assert data["code"] == "INVALID_IMAGE"

    @pytest.mark.asyncio
    async def test_generate_video_from_image_content_blocked(
        self, mock_env, mock_httpx_client, sample_image_bytes
    ):
        """Should handle content blocked error."""
        mock_httpx_client.get("https://example.com/blocked.jpg").mock(
            return_value=httpx.Response(
                200,
                content=sample_image_bytes,
                headers={"content-type": "image/jpeg"},
            )
        )

        mock_httpx_client.post(
            "https://generativelanguage.googleapis.com/v1beta/models/veo-3.1-generate-preview:predictLongRunning"
        ).mock(
            return_value=httpx.Response(400, json={"error": {"message": "Content blocked by safety"}})
        )

        result = await generate_video_from_image({
            "image_url": "https://example.com/blocked.jpg",
            "prompt": "Animate",
        })
        data = json.loads(result[0].text)

        assert data["error"] is True
