"""Pytest configuration and shared fixtures for videogen-mcp.

Generated by GRIMLOCK MCP Factory
"""

import os
from typing import Generator
from unittest.mock import patch

import httpx
import pytest
import respx


@pytest.fixture
def mock_env() -> Generator[None, None, None]:
    """Mock environment variables for testing.

    Yields:
        None (environment is mocked for duration of test)
    """
    env_vars = {
        "HEYGEN_API_KEY": "test-heygen-api-key-12345",
        "GEMINI_API_KEY": "test-gemini-api-key-12345",
    }

    with patch.dict(os.environ, env_vars, clear=False):
        yield


@pytest.fixture
def mock_httpx_client() -> Generator[respx.MockRouter, None, None]:
    """Mock httpx client for external API calls.

    Yields:
        respx.MockRouter for configuring mock responses
    """
    with respx.mock(assert_all_called=False) as respx_mock:
        yield respx_mock


# HeyGen fixtures
@pytest.fixture
def sample_avatars_response() -> dict:
    """Sample HeyGen avatars response."""
    return {
        "data": {
            "avatars": [
                {
                    "avatar_id": "Angela-inblackskirt-20220820",
                    "avatar_name": "Angela",
                    "gender": "female",
                    "preview_image_url": "https://files.heygen.ai/avatar/Angela.png",
                    "preview_video_url": "https://files.heygen.ai/avatar/Angela.mp4",
                    "avatar_type": "stock",
                },
                {
                    "avatar_id": "josh_lite3_20230714",
                    "avatar_name": "Josh",
                    "gender": "male",
                    "preview_image_url": "https://files.heygen.ai/avatar/Josh.png",
                    "preview_video_url": "https://files.heygen.ai/avatar/Josh.mp4",
                    "avatar_type": "stock",
                },
                {
                    "avatar_id": "custom-avatar-001",
                    "avatar_name": "My Custom Avatar",
                    "gender": "female",
                    "preview_image_url": "https://files.heygen.ai/custom/001.png",
                    "preview_video_url": "https://files.heygen.ai/custom/001.mp4",
                    "avatar_type": "instant",
                },
            ]
        }
    }


@pytest.fixture
def sample_voices_response() -> dict:
    """Sample HeyGen voices response."""
    return {
        "data": {
            "voices": [
                {
                    "voice_id": "1bd001e7e50f421d891986aad5158bc8",
                    "display_name": "Sara",
                    "name": "Sara",
                    "language": "en-US",
                    "gender": "female",
                    "preview_audio": "https://files.heygen.ai/voice/sara.mp3",
                    "is_cloned": False,
                },
                {
                    "voice_id": "2a40e7a3b5e64a3d8f9c1234567890ab",
                    "display_name": "Michael",
                    "name": "Michael",
                    "language": "en-US",
                    "gender": "male",
                    "preview_audio": "https://files.heygen.ai/voice/michael.mp3",
                    "is_cloned": False,
                },
                {
                    "voice_id": "custom-voice-001",
                    "display_name": "My Cloned Voice",
                    "name": "My Cloned Voice",
                    "language": "en-US",
                    "gender": "male",
                    "preview_audio": "https://files.heygen.ai/voice/custom001.mp3",
                    "is_cloned": True,
                },
            ]
        }
    }


@pytest.fixture
def sample_video_generate_response() -> dict:
    """Sample HeyGen video generation response."""
    return {
        "data": {
            "video_id": "heygen-video-abc123def456",
        }
    }


@pytest.fixture
def sample_video_status_pending_response() -> dict:
    """Sample HeyGen video status response (pending)."""
    return {
        "data": {
            "video_id": "heygen-video-abc123def456",
            "status": "processing",
            "progress": 45,
        }
    }


@pytest.fixture
def sample_video_status_complete_response() -> dict:
    """Sample HeyGen video status response (completed)."""
    return {
        "data": {
            "video_id": "heygen-video-abc123def456",
            "status": "completed",
            "progress": 100,
            "video_url": "https://files.heygen.ai/video/abc123.mp4",
            "thumbnail_url": "https://files.heygen.ai/video/abc123_thumb.jpg",
            "duration": 45,
        }
    }


@pytest.fixture
def sample_templates_response() -> dict:
    """Sample HeyGen templates response."""
    return {
        "data": {
            "templates": [
                {
                    "template_id": "template-001",
                    "name": "Product Demo",
                    "thumbnail_url": "https://files.heygen.ai/template/001.jpg",
                    "description": "Professional product demonstration template",
                    "variables": {
                        "product_name": {"type": "text"},
                        "product_image": {"type": "image"},
                    },
                },
                {
                    "template_id": "template-002",
                    "name": "Company Intro",
                    "thumbnail_url": "https://files.heygen.ai/template/002.jpg",
                    "description": "Corporate introduction template",
                    "variables": {
                        "company_name": {"type": "text"},
                        "tagline": {"type": "text"},
                    },
                },
            ]
        }
    }


@pytest.fixture
def sample_template_details_response() -> dict:
    """Sample HeyGen template details response."""
    return {
        "data": {
            "template_id": "template-001",
            "name": "Product Demo",
            "variables": {
                "product_name": {
                    "type": "text",
                    "properties": {"content": "Product Name Here"},
                },
                "product_image": {
                    "type": "image",
                    "properties": {"url": "https://example.com/product.jpg"},
                },
                "narrator_voice": {
                    "type": "voice",
                    "properties": {"voice_id": "default-voice"},
                },
            },
        }
    }


# Veo fixtures
@pytest.fixture
def sample_veo_generate_response() -> dict:
    """Sample Veo video generation response."""
    return {
        "name": "operations/veo-generate-abc123xyz789",
    }


@pytest.fixture
def sample_veo_status_processing_response() -> dict:
    """Sample Veo operation status (processing)."""
    return {
        "name": "operations/veo-generate-abc123xyz789",
        "done": False,
        "metadata": {
            "progressPercent": 50,
        },
    }


@pytest.fixture
def sample_veo_status_complete_response() -> dict:
    """Sample Veo operation status (completed)."""
    return {
        "name": "operations/veo-generate-abc123xyz789",
        "done": True,
        "response": {
            "generatedSamples": [
                {
                    "video": {
                        "uri": "https://storage.googleapis.com/veo-output/video123.mp4",
                        "encoding": "h264",
                    }
                }
            ]
        },
    }


@pytest.fixture
def sample_veo_status_failed_response() -> dict:
    """Sample Veo operation status (failed)."""
    return {
        "name": "operations/veo-generate-abc123xyz789",
        "done": True,
        "error": {
            "code": 400,
            "message": "Content policy violation detected",
        },
    }


@pytest.fixture
def mock_rate_limit_response(mock_httpx_client: respx.MockRouter) -> respx.Route:
    """Mock rate limit (429) response."""
    return mock_httpx_client.route().mock(
        return_value=httpx.Response(
            429,
            json={"error": "Rate limit exceeded"},
            headers={"Retry-After": "60"},
        )
    )


@pytest.fixture
def mock_server_error_response(mock_httpx_client: respx.MockRouter) -> respx.Route:
    """Mock server error (500) response."""
    return mock_httpx_client.route().mock(
        return_value=httpx.Response(
            500,
            json={"error": "Internal server error"},
        )
    )


@pytest.fixture
def sample_image_bytes() -> bytes:
    """Sample image bytes for image-to-video testing."""
    # Minimal valid 1x1 transparent PNG
    return (
        b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01'
        b'\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89'
        b'\x00\x00\x00\nIDATx\x9cc\x00\x01\x00\x00\x05\x00\x01'
        b'\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82'
    )
