import argparse
import sys
import os
import time

# Cold-start anchor: capture wall-clock as the very first executable
# statement in the entry point. The AppController's startup_timeline()
# reads this from a module-global so the gap between "Python started"
# and "AppController init began" is visible (this is typically the
# largest startup phase: module imports).
_SLOPPY_COLD_START_TS: float = time.time()

project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
 sys.path.insert(0, project_root)

thirdparty = os.path.join(project_root, "thirdparty")
if thirdparty not in sys.path:
 sys.path.insert(0, thirdparty)

os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
os.environ["TOKENIZERS_PARALLELISM"] = "false"

from src.startup_profiler import startup_profiler

with startup_profiler.phase("defer_sugar"):
 from defer.sugar import install as _install_defer
 _install_defer()

parser = argparse.ArgumentParser(description="Manual Slop entry point")
parser.add_argument("--headless", action="store_true", help="Run in headless mode without GUI")
parser.add_argument("--web-host", default=None, help="Enable web mode and bind to this host (e.g., 0.0.0.0)")
parser.add_argument("--web-port", type=int, default=8080, help="Web mode port (default: 8080)")
parser.add_argument("--enable-test-hooks", action="store_true", help="Enable the HookServer on :8999 for external automation")
# Defer parse_args() so `import sloppy` (for _SLOPPY_COLD_START_TS) doesn't
# require CLI args. parse_args() runs at the start of __main__ only.
args: argparse.Namespace = argparse.Namespace()  # type: ignore[assignment]


if __name__ == "__main__":
 args = parser.parse_args()
 if args.web_host is not None:
  with startup_profiler.phase("web_host_imports"):
   from imgui_bundle import hello_imgui
   from src.api_hooks import HookServer
  with startup_profiler.phase("gui_2_import_webhost"):
   from src.gui_2 import App
  with startup_profiler.phase("app_construct"):
   app = App()

  if args.enable_test_hooks:
   hook_server = HookServer(app)
   hook_server.start()

  runner_params = hello_imgui.RunnerParams()
  runner_params.app_window_params.window_title = "Manual Slop (Web)"
  runner_params.app_window_params.borderless = True
  runner_params.imgui_window_params.default_imgui_window_type = hello_imgui.DefaultImGuiWindowType.provide_full_screen_docker_space
  runner_params.app_window_params.restore_previous_window_size = True

  with startup_profiler.phase("hello_imgui_run"):
   hello_imgui.run(runner_params, lambda: app.render_frame())
 elif args.headless:
  with startup_profiler.phase("headless_imports"):
   from src.app_controller import AppController
  with startup_profiler.phase("appcontroller_construct_headless"):
   controller = AppController(headless=True)
  with startup_profiler.phase("appcontroller_run"):
   controller.run()
 else:
  with startup_profiler.phase("gui_2_main_import"):
   from src.gui_2 import main
  with startup_profiler.phase("main_call"):
   main()
