from fastapi.applications import FastAPI
from api.router import (
    health,
    user,
    trial,
    role,
    book,
    book_collection,
    category,
    bot_general,
    bot_specific,
    bot_one
)
from fastapi.middleware.cors import CORSMiddleware
from api.events import register_events
from utils.utils import pipe


def create_instance() -> FastAPI:
    return FastAPI()


def add_middleware(app: FastAPI) -> FastAPI:
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    return app


def init_database(app: FastAPI) -> FastAPI:
    return app


def register_routers(app: FastAPI) -> FastAPI:
    app.include_router(user.router)
    app.include_router(category.router)
    app.include_router(book.router)
    app.include_router(book_collection.router)
    app.include_router(bot_general.router)
    app.include_router(bot_specific.router)
    app.include_router(bot_one.router)
    app.include_router(trial.router) 
    app.include_router(role.router)
    app.include_router(health.router)

    return app


def init_app() -> FastAPI:
    app: FastAPI = pipe(
        create_instance(),
        add_middleware,
        init_database,
        register_events,
        register_routers,
    )
    return app


app = init_app()