"""kollabor-webui: Hacker-style terminal web UI for Kollabor Engine."""

from pathlib import Path
import webbrowser
import asyncio
import os

STATIC_DIR = Path(__file__).parent / "static"


def main():
    """Launch the Kollabor UI web server."""
    import uvicorn
    from .server import create_app
    import threading
    import time
    
    port = int(os.getenv("KOLLABOR_WEBUI_PORT", "8080"))
    engine_url = os.getenv("KOLLABOR_ENGINE_URL", "http://127.0.0.1:7433")
    
    print(f"\n  kollabor-webui starting...")
    print(f"  engine: {engine_url}")
    print(f"  ui:     http://127.0.0.1:{port}\n")
    
    # Open browser after short delay
    def open_browser_delayed():
        time.sleep(1.5)
        webbrowser.open(f"http://127.0.0.1:{port}")
    
    browser_thread = threading.Thread(target=open_browser_delayed, daemon=True)
    browser_thread.start()
    
    uvicorn.run(
        create_app(engine_url),
        host="127.0.0.1",
        port=port,
        log_level="warning",
    )


if __name__ == "__main__":
    main()
