"""Tests for OCR service health and core functionality."""
import io

import pytest
from fastapi.testclient import TestClient
from PIL import Image

from app.main import app


@pytest.fixture
def client():
    """Create test client for FastAPI app."""
    return TestClient(app)


def test_health_endpoint(client):
    """Health endpoint returns healthy status."""
    response = client.get("/health")
    assert response.status_code == 200
    assert response.json() == {"status": "healthy"}


def test_root_endpoint(client):
    """Root endpoint returns service information."""
    response = client.get("/")
    assert response.status_code == 200
    data = response.json()
    assert data["service"] == "mvp-ocr"
    assert "version" in data


def test_pillow_heif_can_register():
    """pillow-heif can register with Pillow for HEIC support."""
    import pillow_heif

    pillow_heif.register_heif_opener()
    # Verify HEIC format is registered
    assert "HEIF" in Image.registered_extensions().values()


def test_paddleocr_engine_available():
    """PaddleOCR engine can be created."""
    from app.engines.paddle_engine import PaddleOcrEngine

    engine = PaddleOcrEngine()
    assert engine.name == "paddleocr"
