"""Pytest configuration and fixtures."""

import os
import sys
from pathlib import Path

import pytest
from dotenv import load_dotenv

# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

# Load .env file for tests
load_dotenv(dotenv_path=project_root / ".env")

# Set default log level for tests
os.environ.setdefault("LOG_LEVEL", "DEBUG")


@pytest.fixture
def api_token():
    """Get API token from environment for integration tests."""
    token = os.environ.get("ACEDATACLOUD_API_TOKEN", "")
    if not token:
        pytest.skip("ACEDATACLOUD_API_TOKEN not configured for integration tests")
    return token


@pytest.fixture
def mock_image_response():
    """Mock successful image generation response."""
    return {
        "success": True,
        "task_id": "test-task-123",
        "trace_id": "test-trace-456",
        "data": [
            {
                "image_url": "https://cdn.example.com/test-image.png",
                "model": "flux-dev",
                "created_at": "2025-01-21T00:00:00.000Z",
            }
        ],
    }


@pytest.fixture
def mock_video_response():
    """Mock successful video generation response."""
    return {
        "success": True,
        "task_id": "test-task-789",
        "trace_id": "test-trace-012",
        "data": [
            {
                "video_url": "https://cdn.example.com/test-video.mp4",
                "model": "luma",
                "created_at": "2025-01-21T00:00:00.000Z",
            }
        ],
    }


@pytest.fixture
def mock_music_response():
    """Mock successful music generation response."""
    return {
        "success": True,
        "task_id": "test-task-music",
        "trace_id": "test-trace-music",
        "data": [
            {
                "audio_url": "https://cdn.example.com/test-music.mp3",
                "model": "suno",
                "created_at": "2025-01-21T00:00:00.000Z",
            }
        ],
    }


@pytest.fixture
def mock_search_response():
    """Mock successful search response."""
    return {
        "organic": [
            {
                "title": "What is Artificial Intelligence?",
                "link": "https://example.com/ai",
                "snippet": "Artificial intelligence overview.",
            },
            {
                "title": "AI Overview",
                "link": "https://example.com/overview",
                "snippet": "A comprehensive overview.",
            },
        ],
        "knowledge_graph": {
            "title": "Artificial Intelligence",
            "description": "A branch of computer science.",
        },
    }


@pytest.fixture
def mock_task_response():
    """Mock task query response."""
    return {
        "success": True,
        "data": [
            {
                "image_url": "https://cdn.example.com/result.png",
                "model": "flux-dev",
                "state": "succeeded",
                "created_at": "2025-01-21T00:00:00.000Z",
            }
        ],
    }


@pytest.fixture
def mock_error_response():
    """Mock error response."""
    return {
        "success": False,
        "error": {
            "code": "invalid_request",
            "message": "Invalid parameters provided",
        },
    }
