from __future__ import annotations

from typing import TYPE_CHECKING

from fastapi import APIRouter, HTTPException

if TYPE_CHECKING:
    from niannian.client import NianNian


def create_upload_router(nn: NianNian) -> APIRouter:
    router = APIRouter(tags=["upload"])

    @router.get("/v1/upload/progress")
    def upload_progress_active():
        active = nn.upload_progress_active()
        return active if active else {}

    @router.get("/v1/upload/progress/recent")
    def upload_progress_recent(limit: int = 10):
        return {"jobs": nn.upload_progress_recent(limit=limit)}

    @router.get("/v1/upload/progress/{job_id}")
    def upload_progress_job(job_id: str):
        if job_id == "recent":
            raise HTTPException(status_code=404, detail="use /v1/upload/progress/recent")
        job = nn.upload_progress_job(job_id)
        if job is None:
            raise HTTPException(status_code=404, detail="job not found")
        return job

    return router
