#!/usr/bin/env python
"""
Backend server startup script for Windows
Provides better error reporting than running main.py directly
"""
import sys
import os
import uvicorn

# Add backend directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

if __name__ == "__main__":
    print("=" * 60)
    print("Wedding Guest List API Server")
    print("=" * 60)
    
    print("\n[1] Initializing database...")
    try:
        from database import engine
        import models
        models.Base.metadata.create_all(bind=engine)
        print("[OK] Database initialized")
    except Exception as e:
        print(f"[ERROR] Database error: {e}")
        sys.exit(1)
    
    print("\n[2] Importing FastAPI app...")
    try:
        from main import app
        print("[OK] FastAPI app imported")
    except Exception as e:
        print(f"[ERROR] App import error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)
    
    print("\n[3] Starting Uvicorn server...")
    print("   URL: http://localhost:8000")
    print("   Docs: http://localhost:8000/docs")
    print("   ReDoc: http://localhost:8000/redoc")
    print("\nPress Ctrl+C to stop the server")
    print("-" * 60)
    
    try:
        uvicorn.run(
            app,
            host="0.0.0.0",
            port=8000,
            log_level="info",
            access_log=True,
        )
    except KeyboardInterrupt:
        print("\n\nServer stopped by user")
    except Exception as e:
        print(f"\n[ERROR] Server error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)
