from contextlib import asynccontextmanager

from fastapi import FastAPI

from app.config import get_settings
from app.errors import GatewayError, gateway_error_handler
from app.routes.router import api_router
from app.schemas.common import ErrorResponse


@asynccontextmanager
async def lifespan(_: FastAPI):
    yield


def create_app() -> FastAPI:
    settings = get_settings()
    app = FastAPI(
        title="AI Intelligence Gateway",
        version="0.2.0",
        description=(
            "AI Interviewer Pro — AI Intelligence Gateway (Python / FastAPI). "
            "Internal API for the Platform Team (NestJS)."
        ),
        lifespan=lifespan,
        responses={
            401: {"model": ErrorResponse},
            422: {"model": ErrorResponse},
            500: {"model": ErrorResponse},
        },
    )
    app.add_exception_handler(GatewayError, gateway_error_handler)
    app.include_router(api_router, prefix=settings.api_prefix)
    return app


app = create_app()
