#!/usr/bin/env python3
# 最基础的FastAPI测试服务器

import sys
import os

print("🔍 检查Python环境...")
print(f"Python版本: {sys.version}")
print(f"当前目录: {os.getcwd()}")

# 测试导入
try:
    import fastapi
    print(f"✅ FastAPI可用: {fastapi.__version__}")
except ImportError as e:
    print(f"❌ FastAPI不可用: {e}")
    print("正在安装FastAPI...")
    import subprocess
    subprocess.run([sys.executable, "-m", "pip", "install", "fastapi", "uvicorn"])
    import fastapi
    print(f"✅ FastAPI安装成功: {fastapi.__version__}")

try:
    import uvicorn
    print(f"✅ Uvicorn可用: {uvicorn.__version__}")
except ImportError as e:
    print(f"❌ Uvicorn不可用: {e}")
    print("正在安装Uvicorn...")
    import subprocess
    subprocess.run([sys.executable, "-m", "pip", "install", "uvicorn"])
    import uvicorn
    print(f"✅ Uvicorn安装成功: {uvicorn.__version__}")

# 创建最简单的FastAPI应用
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI(
    title="云盘应用测试",
    description="测试服务器",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

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

@app.get("/")
async def root():
    return {"message": "服务器运行正常", "port": 8080, "status": "ok"}

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

@app.get("/api/v1/health")
async def health():
    import time
    return {"status": "healthy", "timestamp": time.time(), "server": "port 8080"}

if __name__ == "__main__":
    print("🚀 启动测试服务器...")
    print("=" * 40)
    print("📍 本地访问: http://localhost:8080")
    print("📚 API文档: http://localhost:8080/docs")
    print("=" * 40)
    print("按 Ctrl+C 停止服务")

    try:
        uvicorn.run(app, host="0.0.0.0", port=8080, log_level="info")
    except Exception as e:
        print(f"❌ 启动失败: {e}")