# Generated by smithplates HTTP codegen. Do not edit.

from __future__ import annotations

from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from generated.warehouse_api.warehouse_api.api_exception_handler import FallbackApiExceptionHandler
from generated.warehouse_api.warehouse_api.api_exceptions import NotImplementedApiError, ServiceUnavailableApiError
from generated.warehouse_api.warehouse_api.apis.warehouse_api import router as WarehouseApiRouter
from generated.warehouse_api.warehouse_api.app_services import ApiServices


def configure_fallback_exception_handlers(
    app: FastAPI,
    fallback_exception_handler: FallbackApiExceptionHandler,
) -> None:
    @app.exception_handler(NotImplementedApiError)
    async def on_not_implemented(
        request: Request,
        exc: NotImplementedApiError,
    ) -> JSONResponse:
        return await fallback_exception_handler.handle_not_implemented(request, exc)

    @app.exception_handler(ServiceUnavailableApiError)
    async def on_service_unavailable_api_error(
        request: Request,
        exc: ServiceUnavailableApiError,
    ) -> JSONResponse:
        return await fallback_exception_handler.handle_service_unavailable_api_error(request, exc)

    @app.exception_handler(RequestValidationError)
    async def on_validation_error(
        request: Request,
        exc: RequestValidationError,
    ) -> JSONResponse:
        return await fallback_exception_handler.handle_validation_error(request, exc)

    @app.exception_handler(Exception)
    async def on_unexpected_error(request: Request, exc: Exception) -> JSONResponse:
        return await fallback_exception_handler.handle_unexpected(request, exc)


def configure_services(app: FastAPI, services: ApiServices) -> None:
    app.state.api_services = services
    app.include_router(WarehouseApiRouter)


def create_app(
    services: ApiServices,
    fallback_exception_handler: FallbackApiExceptionHandler,
) -> FastAPI:
    app = FastAPI(
        title="WarehouseApi",
        description=None,
        version="1",
    )
    configure_services(app, services)
    configure_fallback_exception_handlers(app, fallback_exception_handler)
    return app
