"""
Integration tests for NanoBanana MCP Server.

These tests make REAL API calls to verify all tools work correctly.
Run with: pytest tests/test_integration.py -v -s

Note: These tests require ACEDATACLOUD_API_TOKEN to be set.
They are skipped in CI environments without the token.
"""

import json
import os
import sys

import pytest

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Load environment variables
from dotenv import load_dotenv

load_dotenv()

# Check if API token is configured
HAS_API_TOKEN = bool(os.getenv("ACEDATACLOUD_API_TOKEN"))

# Decorator to skip tests that require API token
requires_api_token = pytest.mark.skipif(
    not HAS_API_TOKEN,
    reason="ACEDATACLOUD_API_TOKEN not configured - skipping integration test",
)


class TestImageTools:
    """Integration tests for image generation tools."""

    @requires_api_token
    @pytest.mark.asyncio
    async def test_generate_image_basic(self) -> None:
        """Test basic image generation with real API."""
        from tools.image_tools import nanobanana_generate_image

        result = await nanobanana_generate_image(
            prompt="A simple red circle on a white background, minimalist",
            model="nano-banana",
            aspect_ratio="1:1",
        )

        print("\n=== Generate Image Result ===")
        print(result)

        assert "task_id" in result


class TestTaskTools:
    """Integration tests for task query tools."""

    @requires_api_token
    @pytest.mark.asyncio
    async def test_get_task_with_real_id(self) -> None:
        """Test querying a task - first generate, then query."""
        from tools.image_tools import nanobanana_generate_image
        from tools.task_tools import nanobanana_get_task

        # Generate an image first
        gen_result = await nanobanana_generate_image(
            prompt="A simple blue square, minimal",
            model="nano-banana",
            aspect_ratio="1:1",
        )

        print("\n=== Generate Result ===")
        print(gen_result)

        gen_data = json.loads(gen_result)
        task_id = gen_data.get("task_id")
        if task_id:
            # Query the task
            task_result = await nanobanana_get_task(task_id=task_id)
            print("\n=== Task Result ===")
            print(task_result)
            assert task_id in task_result
