"""Pytest configuration and shared fixtures."""
import pytest
import asyncio
from typing import Generator
from unittest.mock import AsyncMock, Mock

# Fixtures available to all tests


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


@pytest.fixture
def mock_pm3_worker():
    """Create a mock PM3Worker."""
    worker = Mock()
    worker.is_connected = AsyncMock(return_value=True)
    worker.execute_command = AsyncMock()
    worker.connect = AsyncMock()
    worker.disconnect = AsyncMock()
    worker.device_path = "/dev/ttyACM0"
    return worker


@pytest.fixture
def mock_session_manager():
    """Create a mock SessionManager."""
    manager = Mock()
    manager.can_execute = Mock(return_value=True)
    manager.update_activity = Mock()
    manager.has_active_session = Mock(return_value=False)
    manager.create_session = Mock(return_value="test-session-id")
    manager.release_session = Mock()
    manager.get_session = Mock(return_value=None)
    manager.get_active_session = Mock(return_value=None)
    return manager


@pytest.fixture
def mock_wifi_manager():
    """Create a mock WiFiManager."""
    from app.backend.managers.wifi_manager import WiFiMode, WiFiStatus, WiFiInterface

    manager = Mock()
    manager.get_status = AsyncMock(return_value=WiFiStatus(
        mode=WiFiMode.AP,
        interfaces=[],
        current_ssid=None,
        current_ip=None,
        ap_ssid="raspi-webgui",
        ap_ip="10.3.141.1",
        supports_dual=False
    ))
    manager.scan_networks = AsyncMock(return_value=[])
    manager.connect_to_network = AsyncMock(return_value=True)
    manager.disconnect_from_network = AsyncMock(return_value=True)
    manager.set_mode = AsyncMock(return_value=True)
    manager.get_saved_networks = AsyncMock(return_value=[])
    manager.forget_network = AsyncMock(return_value=True)
    return manager


@pytest.fixture
def mock_update_manager():
    """Create a mock UpdateManager."""
    from app.backend.managers.update_manager import UpdateProgress, UpdateStatus

    manager = Mock()
    manager.check_for_updates = AsyncMock(return_value={
        "update_available": False,
        "current_version": "1.0.0",
        "message": "System is up to date"
    })
    manager.download_update = AsyncMock(return_value=True)
    manager.install_update = AsyncMock(return_value=True)
    manager.get_progress = AsyncMock(return_value=UpdateProgress(
        status=UpdateStatus.IDLE,
        current_version="1.0.0"
    ))
    manager.get_release_notes = AsyncMock(return_value="Release notes")
    return manager


@pytest.fixture
def mock_pm3_service():
    """Create a mock PM3Service for integration tests."""
    service = Mock()
    service.execute_command = AsyncMock()
    service.get_status = AsyncMock()
    service.connect = AsyncMock()
    service.disconnect = AsyncMock()
    service.create_session = Mock()
    service.release_session = Mock()
    service.get_session_info = Mock()
    return service


@pytest.fixture
def test_client():
    """Create a FastAPI TestClient for integration tests."""
    from fastapi.testclient import TestClient
    from app.backend.main import app

    return TestClient(app)
