#!/usr/bin/env python3
print("🚀 启动云盘应用服务器...")

# 检查Python环境
import sys
print(f"Python版本: {sys.version}")

# 尝试导入FastAPI
try:
    from fastapi import FastAPI
    from fastapi.middleware.cors import CORSMiddleware
    import uvicorn
    print("✅ FastAPI依赖可用")

    # 创建FastAPI应用
    app = FastAPI(
        title="云盘应用 API",
        description="现代化的云存储Web应用后端API",
        version="1.0.0",
        docs_url="/docs",
        redoc_url="/redoc"
    )

    # 添加CORS中间件
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # 定义路由
    @app.get("/")
    async def root():
        return {
            "message": "云盘应用 API",
            "version": "1.0.1",
            "docs": "/docs",
            "health": "/api/v1/health"
        }

    @app.get("/health")
    async def health():
        return {"status": "healthy"}

    @app.get("/api/v1/health")
    async def api_health():
        import time
        return {
            "status": "healthy",
            "timestamp": time.time(),
            "version": "1.0.0"
        }

    @app.get("/test")
    async def test():
        return {"test": "ok", "server": "working"}

    # 尝试导入app模块以提供完整功能
    try:
        from app.core.config import settings
        print("✅ 完整app模块可用")
        mode = "full"
    except ImportError:
        print("⚠️  使用简化模式")
        mode = "simplified"

    @app.get("/info")
    async def info():
        return {
            "mode": mode,
            "python_version": str(sys.version),
            "status": "running"
        }

    print("=" * 50)
    print("📍 服务器地址:")
    print("   http://localhost:8080")
    print("   http://localhost:8080/docs")
    print("   http://localhost:8080/api/v1/health")
    print("=" * 50)
    print("按 Ctrl+C 停止服务")
    print()

    # 启动服务器
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=8080,
        reload=False,
        access_log=True,
        log_level="info"
    )

except ImportError as e:
    print(f"❌ 依赖导入失败: {e}")
    print("请运行: pip install fastapi uvicorn")
    sys.exit(1)
except Exception as e:
    print(f"❌ 启动失败: {e}")
    sys.exit(1)