from fastapi import FastAPI
from .api import vector, raster, cd_prediction
from .database import engine, Base
from fastapi.middleware.cors import CORSMiddleware

# 创建数据库表
Base.metadata.create_all(bind=engine)

app = FastAPI(
    title="地图数据处理系统",
    description="一个用于处理地图数据的API系统",
    version="1.0.0",
    openapi_tags=[
        {
            "name": "vector",
            "description": "矢量数据相关接口",
        },
        {
            "name": "raster",
            "description": "栅格数据相关接口",
        },
        {
            "name": "cd-prediction",
            "description": "Cd预测模型相关接口",
        }
    ]
)

# ---------------------------
# 添加 CORS 配置（关键修改）
# ---------------------------
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://soilgd.com"],  # 允许的前端域名（需与前端实际域名一致）
    allow_methods=["*"],                   # 允许的 HTTP 方法（GET/POST/PUT/DELETE等）
    allow_headers=["*"],                   # 允许的请求头
    allow_credentials=True,                # 允许携带 Cookie（如需）
)

# 注册路由
app.include_router(vector.router, prefix="/api/vector", tags=["vector"])
app.include_router(raster.router, prefix="/api/raster", tags=["raster"])
app.include_router(cd_prediction.router, prefix="/api/cd-prediction", tags=["cd-prediction"])

@app.get("/")
async def root():
    return {"message": "Welcome to the GIS Data Management API"}

# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8000)
