"""
Entry point for the unified LF-MRI GUI.

Run from the repo root (MRI-testing/):
    python lf_mri_gui/app.py

Or with optional pre-loads:
    python lf_mri_gui/app.py path/to/sequence.seq \\
        --hw-config lf_mri_gui/cfg/hw_config.json \\
        --output-dir /tmp/mri_output
"""
from __future__ import annotations

import os
import sys

# -- sys.path setup ------------------------------------------------------------
# _here            = apps/gui/        -> enables: from src.xxx import ...
# _repo_root       = repo root/       -> enables: local project imports
# _lf_scanner_root = libs/lf-scanner/ -> enables: import pypulseq
_here = os.path.dirname(os.path.abspath(__file__))
_repo_root = os.path.dirname(os.path.dirname(_here))
_lf_scanner_root = os.path.join(_repo_root, "libs", "lf-scanner")

for _p in (_here, _repo_root, _lf_scanner_root):
    if _p not in sys.path:
        sys.path.insert(0, _p)

import argparse

from PySide6.QtWidgets import QApplication

from src.app_window import LFMRIWindow


def main() -> None:
    parser = argparse.ArgumentParser(
        description="LF-MRI System - unified scanner GUI"
    )
    parser.add_argument(
        "seq_file", nargs="?", default=None,
        help="Optional .seq file to pre-load in the Sequence tab",
    )
    parser.add_argument(
        "--hw-config", default=None,
        help="Optional hardware config JSON to load on startup",
    )
    parser.add_argument(
        "--output-dir", default=None,
        help="Optional output directory for exports",
    )
    args = parser.parse_args()

    app = QApplication(sys.argv)
    app.setApplicationName("LF-MRI System")
    app.setOrganizationName("LF-MRI")

    hw_config = os.path.abspath(args.hw_config)  if args.hw_config  and os.path.isfile(args.hw_config)  else None
    output_dir = os.path.abspath(args.output_dir) if args.output_dir else None
    seq_file  = os.path.abspath(args.seq_file)   if args.seq_file   and os.path.isfile(args.seq_file)   else None

    # Load service URLs and mode from bundled config (fallback to defaults)
    _cfg_path = os.path.join(_here, "cfg", "server_config.json")
    orchestrator_url = "http://localhost:1717"
    seq_interp_url   = "http://localhost:7475"
    spectroscopy_url = "http://localhost:8002"
    spectrometer_url = "http://localhost:1818"
    hw_mode          = "plug"
    try:
        import json as _json
        with open(_cfg_path, encoding="utf-8") as _f:
            _cfg = _json.load(_f)
        orchestrator_url = _cfg.get("orchestrator_url", orchestrator_url)
        seq_interp_url   = _cfg.get("seq_interp_url",   seq_interp_url)
        spectroscopy_url = _cfg.get("spectroscopy_url", spectroscopy_url)
        spectrometer_url = _cfg.get("spectrometer_url", spectrometer_url)
        hw_mode          = _cfg.get("mode", hw_mode)
    except Exception:
        pass

    win = LFMRIWindow(
        hw_config_path=hw_config,
        output_dir=output_dir,
        seq_file=seq_file,
        orchestrator_url=orchestrator_url,
        seq_interp_url=seq_interp_url,
        spectroscopy_url=spectroscopy_url,
        spectrometer_url=spectrometer_url,
        hw_mode=hw_mode,
    )
    # Start in a maximized windowed state so the app fills the usable screen
    # area while preserving the native title bar and taskbar.
    win.showMaximized()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()
