# ========================
# == FastAPI entrypoint ==
# ========================

from fastapi import FastAPI, HTTPException
from .schemas import StockEvent, AvailabilityRequest, ReservationRequest
from .Ledger_logic import process_stock_event, check_availability, reserve_inventory
from .mcp_Client import init_mcp, notify_agents

app = FastAPI(title="Stock Ledger Agent (CSV Mode)")

@app.on_event("startup")
async def startup():
    await init_mcp()

@app.post("/process_event")
async def process_event(event: StockEvent):
    try:
        result = process_stock_event(event)
        await notify_agents(result)
        return result
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@app.post("/check_availability")
async def availability(request: AvailabilityRequest):
    return check_availability(request)

@app.post("/reserve_inventory")
async def reserve(request: ReservationRequest):
    return reserve_inventory(request)
