from fastapi import APIRouter
from celery.result import AsyncResult
from app.tasks.worker import celery_app

router = APIRouter()

@router.get("/tasks/{task_id}")
async def get_task_status(task_id: str):
    res = AsyncResult(task_id, app=celery_app)
    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
