"""Pytest configuration and fixtures"""

import pytest
import asyncio
from typing import AsyncGenerator
from unittest.mock import AsyncMock, MagicMock, patch

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker


@pytest.fixture(scope="session")
def event_loop():
    """Create event loop for async tests"""
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()


@pytest.fixture
def mock_db_session():
    """Mock database session"""
    session = AsyncMock(spec=AsyncSession)
    session.add = MagicMock()
    session.commit = AsyncMock()
    session.execute = AsyncMock()
    session.refresh = AsyncMock()
    session.close = AsyncMock()
    return session


@pytest.fixture
def sample_top500_response():
    """Sample TOP500 API response"""
    return {
        "items": [
            {
                "rank": 1,
                "system_name": "Frontier",
                "country": "USA",
                "city": "Oak Ridge",
                "latitude": 35.9322,
                "longitude": -84.3108,
                "manufacturer": "HPE",
                "r_max": 1102000.0,
                "r_peak": 1685000.0,
                "power": 21510.0,
                "cores": 8730112,
                "interconnect": "Slingshot 11",
                "os": "CentOS",
            },
            {
                "rank": 2,
                "system_name": "Fugaku",
                "country": "Japan",
                "city": "Kobe",
                "latitude": 34.6913,
                "longitude": 135.1830,
                "manufacturer": "Fujitsu",
                "r_max": 442010.0,
                "r_peak": 537212.0,
                "power": 29899.0,
                "cores": 7630848,
                "interconnect": "Tofu interconnect D",
                "os": "RHEL",
            },
        ]
    }


@pytest.fixture
def sample_huggingface_response():
    """Sample Hugging Face API response"""
    return {
        "models": [
            {
                "id": "bert-base-uncased",
                "author": "google",
                "description": "BERT base model",
                "likes": 25000,
                "downloads": 5000000,
                "language": "en",
                "tags": ["transformer", "bert"],
                "pipeline_tag": "feature-extraction",
                "library_name": "transformers",
                "createdAt": "2024-01-15T10:00:00Z",
            }
        ]
    }


@pytest.fixture
def sample_alert_data():
    """Sample alert data"""
    return {
        "id": 1,
        "severity": "warning",
        "status": "active",
        "datasource_id": 2,
        "datasource_name": "Epoch AI",
        "message": "API response time > 30s",
        "created_at": "2024-01-20T09:30:00Z",
        "acknowledged_by": None,
    }
