from fastapi import APIRouter
from pydantic import BaseModel
from app.services import cluster_inventory, 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(cluster_inventory.get_network_status())
    alerts_task  = asyncio.create_task(alertmanager.get_alerts())
    network_state, alerts = await asyncio.gather(nfs_task, alerts_task)
    return network_state, alerts
