"""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_tesseract_available():
    """Tesseract OCR is available and can process images."""
    import pytesseract

    # Create a simple test image with text
    img = Image.new("RGB", (200, 50), color="white")

    # Verify pytesseract can call tesseract (will return empty string for blank image)
    result = pytesseract.image_to_string(img)
    # Just verify it doesn't raise an exception - blank image returns empty/whitespace
    assert isinstance(result, str)
