import logging
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from app.providers import (
    logging_provider,
    app_provider,
    handle_exception,
    pagination_provider,
    route_provider,
    auth_provider,
)
from config.config import settings
import uvicorn


def create_app() -> FastAPI:
    _app = FastAPI()

    register(_app, logging_provider)
    register(_app, app_provider)
    register(_app, handle_exception)
    register(_app, pagination_provider)
    register(_app, auth_provider)

    boot(_app, route_provider)

    return _app


def register(_app, provider):
    provider.register(_app)
    logging.info(provider.__name__ + " registered")


def boot(_app, provider):
    provider.boot(_app)
    logging.info(provider.__name__ + " booted")


app = create_app()


@app.get("/")
async def root():
    return "Welcome to Open Assistant Api"


if __name__ == "__main__":
    # app.run(host="0.0.0.0", port=settings.SERVER_PORT)
    uvicorn.run(
        app=app,  # "main:app",
        host=settings.SERVER_HOST,
        port=settings.SERVER_PORT,
        # workers=settings.SERVER_WORKERS,
        reload=settings.ENV == "local",
    )


# 添加全局异常处理器
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc: HTTPException):
    return JSONResponse(
        status_code=exc.status_code,
        content={"detail": exc.detail, "message": "Resource not found."},
    )
