import os
from fastapi import APIRouter

router = APIRouter()

# Task status endpoint dung chung cho ca 2 che do:
# - Server/Docker: celery (AsyncResult, broker Redis).
# - Desktop slim (PyInstaller khong bundle celery): in-process registry
#   (app/tasks/worker._SimpleAsyncResult) — API contract giong het nhau.


@router.get("/tasks/{task_id}")
async def get_task_status(task_id: str):
    from app.tasks.worker import get_task_result

    res = get_task_result(task_id)
    response_data = {
        "task_id": task_id,
        "status": res.status,
    }
    if res.ready():
        if res.successful():
            response_data["result"] = res.result
        else:
            response_data["error"] = str(res.result)
    return response_data
