#!/usr/bin/env python3
"""
Script to start the image detection backend server
"""

import os
import sys
import subprocess

def start_server():
    """Start the FastAPI server"""
    try:
        # Change to the app directory
        script_dir = os.path.dirname(os.path.abspath(__file__))
        app_dir = os.path.join(script_dir, "app")
        
        # Import config to get HOST and PORT from environment variables
        sys.path.insert(0, script_dir)
        from app.config import HOST, PORT
        
        print("Starting image detection backend server...")
        print(f"Server will be available at http://{HOST}:{PORT}")
        
        # Start the server using uvicorn with proper module path
        # Use HOST and PORT from config (which reads from env variables)
        subprocess.run([
            sys.executable, "-m", "uvicorn", 
            "main:app", 
            "--host", HOST, 
            "--port", str(PORT),
            "--reload"
        ], cwd=script_dir)
        
    except KeyboardInterrupt:
        print("\nServer stopped.")
    except Exception as e:
        print(f"Error starting server: {e}")
        sys.exit(1)

if __name__ == "__main__":
    start_server()