from fastapi import APIRouter, BackgroundTasks, HTTPException

from app.models.document import IngestRequest, IngestResponse
from app.models.parse_run import ParseRunStatus
from app.repositories import parse_run_repo
from app.services import ingestion_service
from app.workers.ingestion_worker import run_ingestion_job

router = APIRouter(prefix="/ingest", tags=["Ingestion"])


@router.post("/", response_model=IngestResponse, status_code=202)
async def ingest_document(
    request: IngestRequest,
    background_tasks: BackgroundTasks,
) -> IngestResponse:
    """
    Queue a repository document for parsing and chunking.
    Returns immediately with a `run_id`; poll `/ingest/status/{run_id}` to track progress.
    """
    run = await ingestion_service.create_parse_run(request)
    background_tasks.add_task(run_ingestion_job, run.run_id, request)
    return IngestResponse(
        run_id=run.run_id,
        status=ParseRunStatus.PENDING,
        message=f"Queued: {request.doc_path}",
    )


@router.get("/status/{run_id}")
async def get_ingestion_status(run_id: str):
    """Retrieve the status and result of an ingestion run."""
    run = await parse_run_repo.get_run_by_id(run_id)
    if not run:
        raise HTTPException(status_code=404, detail="Run not found")
    return run