from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles

from app.api.v1.router import api_router
from app.config import get_settings
from app.core.branding_images import IMAGES_DIR, ensure_images_dir
from app.middleware.api_crypto import ApiCryptoMiddleware
from app.redis_client import close_redis, init_redis
from app.schemas.common import ApiResponse

settings = get_settings()


@asynccontextmanager
async def lifespan(_app: FastAPI):
    await init_redis()
    yield
    await close_redis()


def create_app() -> FastAPI:
    app = FastAPI(
        title=settings.app_name,
        debug=settings.debug,
        lifespan=lifespan,
    )

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

    @app.get("/health", response_model=ApiResponse[dict])
    async def health() -> ApiResponse[dict]:
        return ApiResponse(ok=True, message="服务正常", data={"status": "up"})

    ensure_images_dir()
    app.mount(
        "/images",
        StaticFiles(directory=str(IMAGES_DIR)),
        name="branding-images",
    )

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


app = create_app()
