"""PyInstaller entry point for the ProcessMonitor analyst GUI."""

import os
import json
from pathlib import Path

from gui.app import ProcessMonitorApp, main


def packaged_main() -> int:
    smoke_ms = os.environ.get("PROCESSMONITOR_GUI_SMOKE_MS")
    if not smoke_ms:
        return main()
    app = ProcessMonitorApp()
    if os.environ.get("PROCESSMONITOR_GUI_RESIZE_SMOKE"):
        def exercise_resize():
            try:
                sizes = []
                for geometry in ("1080x680", "1280x720", "1480x900", "1920x1080", "2200x1200"):
                    app.geometry(geometry)
                    app.update_idletasks()
                    sizes.append({"requested": geometry, "actual": [app.winfo_width(), app.winfo_height()]})
                for page in app.pages:
                    app.show_page(page)
                    app.update_idletasks()
                app.state("zoomed")
                app.update_idletasks()
                maximized = app.state()
                app.state("normal")
                app.update_idletasks()
                result = {"sizes": sizes, "pages": list(app.pages), "maximized": maximized, "restored": app.state()}
            except Exception as exc:
                result = {"error": str(exc)}
            evidence = os.environ.get("PROCESSMONITOR_GUI_RESIZE_SMOKE")
            if evidence and evidence != "1":
                Path(evidence).write_text(json.dumps(result, indent=2), encoding="utf-8")
        app.after(250, exercise_resize)
    app.after(max(100, int(smoke_ms)), app._close)
    app.mainloop()
    return 0


if __name__ == "__main__":
    raise SystemExit(packaged_main())
