#!/usr/bin/env python3
"""
Launcher script for the Eatance Website Generator
Runs the FastAPI application from the src/ directory
"""

import sys
import os
import subprocess

if __name__ == '__main__':
    import socket
    import sys
    sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
    from core.config import settings
    
    # Get port from environment variable
    app_port = int(os.getenv("APP_PORT", "8000"))
    
    print("=== Starting Eatance Website Generator ===")
    print(f"Python version: {sys.version}")
    print(f"Working directory: {os.getcwd()}")
    
    # Get local IP address
    try:
        # Connect to a remote address to get local IP
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        local_ip = s.getsockname()[0]
        s.close()
        
        # Use auto-detection for proper protocol and domain
        network_url = settings.get_current_domain().replace("localhost", local_ip)
        local_url = settings.get_current_domain()
        
        print(f"Starting FastAPI server on {local_ip}:{app_port}")
        print(f"🌐 Network URL: {network_url}")
        print(f"🏠 Local URL: {local_url}")
        print(f"📱 Share this URL with your team: {network_url}")
    except Exception as e:
        local_url = settings.get_current_domain()
        print(f"Starting FastAPI server on localhost:{app_port}")
        print(f"🌐 Local URL: {local_url}")
        print("Could not determine network IP automatically")
    
    # Run the FastAPI app directly
    script_path = os.path.join(os.path.dirname(__file__), 'src', 'main.py')
    subprocess.run([sys.executable, script_path])
