# services/comfy-agent/app/main.py
import asyncio
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from .config import settings
from .api import router
from .worker import worker_loop
from .nats_client import start_nats
from .storage import ensure_storage, init_object_storage
from .idempotency import init_idempotency_store

app = FastAPI(title="Comfy Agent Service", version="0.1.0")
app.include_router(router)

@app.on_event("startup")
async def startup():
    ensure_storage()
    init_object_storage()
    init_idempotency_store()

    # Static files for result URLs: /files/{job_id}/...
    app.mount("/files", StaticFiles(directory=settings.STORAGE_PATH), name="files")

    asyncio.create_task(worker_loop())
    await start_nats()

@app.get("/healthz")
async def healthz():
    return {"ok": True, "service": settings.SERVICE_NAME}
