# fitz_ai/api/routes/ingest.py
"""Document ingestion endpoints."""

from __future__ import annotations

from fastapi import APIRouter

from fitz_ai.api.dependencies import get_fitz_instance
from fitz_ai.api.error_handlers import handle_api_errors
from fitz_ai.api.models.schemas import IngestRequest, IngestResponse

router = APIRouter(tags=["ingest"])


@router.post("/ingest", response_model=IngestResponse)
@handle_api_errors
async def ingest(request: IngestRequest) -> IngestResponse:
    """
    Ingest documents into the knowledge base.

    Provide a path to a file or directory. Documents will be chunked,
    embedded, and stored in the vector database.
    """
    f = get_fitz_instance(request.collection)
    stats = f.ingest(request.source, clear_existing=request.clear_existing)

    return IngestResponse(
        documents=stats.documents,
        chunks=stats.chunks,
        collection=stats.collection,
    )
