"""Enrichment job status API."""

from __future__ import annotations

from typing import TYPE_CHECKING

from fastapi import APIRouter, HTTPException, Request

from niannian.chat.enrichment_jobs import EnrichmentJobStore
from niannian.tenant.context import resolve_request_context

if TYPE_CHECKING:
    from niannian.client import NianNian


def create_chat_jobs_router(nn: NianNian) -> APIRouter:
    router = APIRouter(prefix="/v1/chat", tags=["chat"])
    store = EnrichmentJobStore(nn._home)

    @router.get("/enrichment-jobs/{job_id}")
    def get_enrichment_job(request: Request, job_id: str):
        resolve_request_context(request, nn._config, home=nn._home)
        record = store.get(job_id)
        if record is None:
            raise HTTPException(status_code=404, detail={"code": "job_not_found"})
        return record.to_dict()

    return router
