from fastapi import APIRouter
from pydantic import BaseModel
from app.services import prometheus, alertmanager, ai

router = APIRouter()


class QueryRequest(BaseModel):
    query: str


@router.post("/query")
async def query(req: QueryRequest):
    network_state, alerts = await _gather(req.query)
    response = await ai.answer(req.query, network_state, alerts)
    return {"response": response, "network_state": network_state, "alerts": alerts}


async def _gather(query_text: str):
    import asyncio
    nfs_task     = asyncio.create_task(prometheus.get_nf_status())
    alerts_task  = asyncio.create_task(alertmanager.get_alerts())
    nfs, alerts  = await asyncio.gather(nfs_task, alerts_task)
    return {"nfs": nfs}, alerts
