"""
FastAPI 应用入口 — 挂载中间件、路由
"""

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware

from app.api.auth import router as auth_router
from app.core.config import settings

app = FastAPI(title="军事科技工作台", version="0.1.0")

# ---- CORS 中间件 ----
# 显式列 origins 不能用 ['*']，否则 credentials=True 时浏览器拒绝携带 cookie
app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:5173"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ---- Session 中间件 ----
# 密钥来自 .env，用于签名 session cookie
app.add_middleware(
    SessionMiddleware,
    secret_key=settings.SECRET_KEY,
    session_cookie="milsci_session",
    max_age=settings.SESSION_MAX_AGE,
    same_site="lax",
    https_only=False,  # 本地开发用 HTTP
)

# ---- 注册路由 ----
app.include_router(auth_router)