import os
import uvicorn
import argparse
from rich.console import Console
from rich.panel import Panel
from dotenv import load_dotenv

# Load environment variables from .env file if present
load_dotenv()

console = Console()

def main():
    parser = argparse.ArgumentParser(description='Run the OpenAI-compatible API server for DeepInfra')
    parser.add_argument('--host', default='0.0.0.0', help='Host to bind the server to')
    parser.add_argument('--port', type=int, default=8000, help='Port to bind the server to')
    parser.add_argument('--reload', action='store_true', help='Enable auto-reload for development')
    
    args = parser.parse_args()
    
    console.print(Panel.fit(
        "[bold green]DeepInfra OpenAI-Compatible API Server[/bold green]\n"
        f"Starting server on http://{args.host}:{args.port}\n"
        "Press Ctrl+C to stop the server.",
        title="Server Info",
        border_style="blue"
    ))
    
    # Additional info on endpoints
    console.print("[bold cyan]Available endpoints:[/bold cyan]")
    console.print("- [yellow]/v1/models[/yellow] - List available models")
    console.print("- [yellow]/v1/chat/completions[/yellow] - Chat completions endpoint")
    console.print("- [yellow]/v1/completions[/yellow] - Text completions endpoint")
    console.print("- [yellow]/health[/yellow] - Health check endpoint")
    console.print("\nAPI documentation available at [link]http://localhost:8000/docs[/link]")
    
    # Run the server
    uvicorn.run(
        "openai_compatible_api:app", 
        host=args.host, 
        port=args.port, 
        reload=args.reload
    )

if __name__ == "__main__":
    main()