"""HTTP server for: {{ ir.name }}"""
# Generated by agent-blueprint — DO NOT EDIT

import os

try:
    from dotenv import load_dotenv
    load_dotenv(override=False)
except ImportError:
    pass

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import uvicorn

from main import run

app = FastAPI(title="{{ ir.name }}", version="{{ ir.version }}")


@app.post("/invoke")
async def invoke(request: Request):
    """Run the agent with a user message.

    Request body: {"input": "...", "thread_id": "..."}
    Response:     {"response": "..."}
    """
    body = await request.json()
    user_input = body.get("input", "")
    thread_id = body.get("thread_id", "default")
    try:
        response = run(user_input, thread_id=thread_id)
        return JSONResponse({"response": response})
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)


@app.get("/health")
def health():
    return {"status": "ok", "agent": "{{ ir.name }}"}


if __name__ == "__main__":
    port = int(os.environ.get("PORT", 8080))
    uvicorn.run(app, host="0.0.0.0", port=port)
