"""Node Worker — NATS offload executor for cross-node inference."""
import logging
import os

from fastapi import FastAPI

import config
import worker

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("node-worker")

app = FastAPI(title="Node Worker", version="1.0.0")

_nats_client = None


@app.get("/healthz")
async def healthz():
    connected = _nats_client is not None and _nats_client.is_connected if _nats_client else False
    return {
        "status": "ok" if connected else "degraded",
        "node_id": config.NODE_ID,
        "nats_connected": connected,
        "max_concurrency": config.MAX_CONCURRENCY,
    }


@app.get("/metrics")
async def metrics():
    return worker.get_metrics()


@app.get("/prom_metrics")
async def prom_metrics():
    from fastapi.responses import Response
    import fabric_metrics as fm
    data = fm.get_metrics_text()
    if data:
        return Response(content=data, media_type="text/plain; charset=utf-8")
    return {"error": "prometheus_client not installed"}


@app.get("/caps")
async def caps():
    """Capability flags for NCS to aggregate."""
    return {
        "node_id": config.NODE_ID,
        "capabilities": {
            "llm": True,
            "vision": True,
            "stt": config.STT_PROVIDER != "none",
            "tts": config.TTS_PROVIDER != "none",
            "ocr": config.OCR_PROVIDER != "none",
            "image": config.IMAGE_PROVIDER != "none",
        },
        "providers": {
            "stt": config.STT_PROVIDER,
            "tts": config.TTS_PROVIDER,
            "ocr": config.OCR_PROVIDER,
            "image": config.IMAGE_PROVIDER,
        },
        "defaults": {
            "llm": config.DEFAULT_LLM,
            "vision": config.DEFAULT_VISION,
        },
        "concurrency": config.MAX_CONCURRENCY,
    }


@app.on_event("startup")
async def startup():
    global _nats_client
    try:
        import nats as nats_lib
        _nats_client = await nats_lib.connect(config.NATS_URL)
        logger.info(f"✅ NATS connected: {config.NATS_URL}")
        await worker.start(_nats_client)
        logger.info(f"✅ Node Worker ready: node={config.NODE_ID} concurrency={config.MAX_CONCURRENCY}")
    except Exception as e:
        logger.error(f"❌ Startup failed: {e}")


@app.on_event("shutdown")
async def shutdown():
    if _nats_client:
        try:
            await _nats_client.close()
        except Exception:
            pass


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=config.PORT)
