from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.cors import CORSMiddleware
from tortoise.contrib.fastapi import register_tortoise
from tortoise.exceptions import (
    DoesNotExist,
    IntegrityError,
    ValidationError as TortoiseValidationError,
)

from api import api_router
from config import settings
from exceptions.handlers import (
    http_exception_handler,
    global_exception_handler,
    validation_exception_handler,
    database_exception_handler,
)

app = FastAPI(title=settings.APP_NAME, version=settings.VERSION)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册异常处理器
app.add_exception_handler(StarletteHTTPException, http_exception_handler)
app.add_exception_handler(RequestValidationError, validation_exception_handler)
app.add_exception_handler(DoesNotExist, database_exception_handler)
app.add_exception_handler(IntegrityError, database_exception_handler)
app.add_exception_handler(TortoiseValidationError, database_exception_handler)
app.add_exception_handler(Exception, global_exception_handler)


app.include_router(api_router, prefix="/api")


# 定义包含时区的配置字典
tortoise_config = {
    "connections": {"default": settings.DATABASE_URL},
    "apps": {
        "models": {
            "models": [f"models.{module}" for module in __import__("models").__all__]
            + ["aerich.models"],
            "default_connection": "default",
        }
    },
    "use_tz": True,  # 启用时区支持
    "timezone": settings.TIMEZONE,  # 设置为北京时间（+8时区）
}


register_tortoise(
    app,
    config=tortoise_config,  # 传递包含时区的配置
    generate_schemas=settings.GENERATE_SCHEMAS,
    add_exception_handlers=True,
)
