"""
Jobs-Router: Status einzelner Hintergrund-Jobs + Liste der letzten Jobs.
Liefert die Daten fuer das Aktivitaets-Panel mit Live-Log.
"""

from fastapi import APIRouter, Depends, HTTPException

from auth import auth
from jobengine import JOBS

router = APIRouter(prefix="/api", dependencies=[Depends(auth)])


@router.get("/jobs/{job_id}")
def job_status(job_id: str):
    job = JOBS.get(job_id)
    if not job:
        raise HTTPException(404, "Job nicht gefunden.")
    return job


@router.get("/jobs")
def jobs_list():
    return sorted(JOBS.values(), key=lambda j: j["started_at"], reverse=True)[:20]
