"""FastAPI application entrypoint.

Hosts:
  - POST /tank-allocation          (Command API — called by ERP PHP)
  - POST /webhook/fastlane         (Webhook  — called by Fastlane)
  - GET  /health                   (health check)
"""
from contextlib import asynccontextmanager

from fastapi import FastAPI

from .config import get_settings
from .kafka_client import KafkaProducer, get_producer
from .logger import get_logger
from .routers import commands, health, webhook, debug

log = get_logger("btl-fastlane")


@asynccontextmanager
async def lifespan(app: FastAPI):
    log.info("Starting BTL FastLane FastAPI service …")
    await get_producer()  # eager start
    yield
    if KafkaProducer._instance is not None:
        await KafkaProducer._instance.stop()
    log.info("Stopped.")


settings = get_settings()

app = FastAPI(
    title="BTL FastLane Integration",
    version="1.0.0",
    description="Event-driven integration between BTL ERP and BASF FastLane.",
    lifespan=lifespan,
)

app.include_router(health.router)
app.include_router(commands.router)
app.include_router(webhook.router)
app.include_router(debug.router)
