"""
Unit tests for FastAPI app endpoints.
Uses TestClient to test endpoints without starting the server.
"""

import pytest
from fastapi.testclient import TestClient

from src.app import app


client = TestClient(app)


def test_health_endpoint():
    response = client.get("/health")
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "ok"
    assert data["version"] == "0.1.0"


def test_extract_endpoint_minimal():
    response = client.post(
        "/extract",
        json={"text": "We had a meeting today to discuss deadlines"},
    )
    assert response.status_code == 200
    data = response.json()
    assert "extractions" in data
    assert "metadata" in data
    assert data["metadata"]["char_count"] == len("We had a meeting today to discuss deadlines")


def test_extract_endpoint_with_task_id():
    response = client.post(
        "/extract",
        json={
            "text": "John decided to ship by Friday",
            "task_id": "transcript-extraction",
            "model_id": "gemini-2.5-flash",
        },
    )
    assert response.status_code == 200
    data = response.json()
    assert isinstance(data["extractions"], list)


def test_extract_endpoint_rejects_empty_text():
    response = client.post("/extract", json={"text": ""})
    assert response.status_code == 422


def test_extract_endpoint_request_id_forwarding():
    response = client.post(
        "/extract",
        json={"text": "test text for request ID"},
        headers={"x-request-id": "test-req-123"},
    )
    assert response.status_code == 200


def test_extract_batch_endpoint():
    response = client.post(
        "/extract/batch",
        json={
            "requests": [
                {"text": "First document about a meeting"},
                {"text": "Second document with action items to do"},
            ],
        },
    )
    assert response.status_code == 200
    data = response.json()
    assert isinstance(data, list)
    assert len(data) == 2


def test_extract_batch_rejects_empty():
    response = client.post("/extract/batch", json={"requests": []})
    assert response.status_code == 422
