from __future__ import annotations

import os
import sys
import webbrowser
from pathlib import Path
from threading import Timer

import uvicorn

ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT / "backend"))
os.chdir(ROOT)
HOST = os.environ.get("FLEXIFORGE_HOST", "127.0.0.1")
PORT = int(os.environ.get("FLEXIFORGE_PORT", "8876"))


def open_browser() -> None:
    if os.environ.get("FLEXIFORGE_NO_BROWSER") != "1":
        webbrowser.open(f"http://{HOST}:{PORT}")


if __name__ == "__main__":
    Timer(1.2, open_browser).start()
    uvicorn.run("app:app", app_dir=str(ROOT / "backend"), host=HOST, port=PORT, reload=False)
