#!/usr/bin/env python3
print("Starting Cloud Drive Application Server...")

import sys
print(f"Python version: {sys.version}")

try:
    from fastapi import FastAPI
    from fastapi.middleware.cors import CORSMiddleware
    import uvicorn
    print("FastAPI dependencies available")

    app = FastAPI(
        title="Cloud Drive API",
        description="Modern Cloud Storage Web Application Backend API",
        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": "Cloud Drive 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"}

    try:
        from app.core.config import settings
        print("Full app module available")
        mode = "full"
    except ImportError:
        print("Using simplified mode")
        mode = "simplified"

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

    print("=" * 50)
    print("Server URLs:")
    print("   http://localhost:8080")
    print("   http://localhost:8080/docs")
    print("   http://localhost:8080/api/v1/health")
    print("=" * 50)
    print("Press Ctrl+C to stop server")
    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"Dependency import failed: {e}")
    print("Please run: pip install fastapi uvicorn")
    sys.exit(1)
except Exception as e:
    print(f"Startup failed: {e}")
    sys.exit(1)