"""
Entry point for the MRI Sequence Interpreter GUI.

Run from the repo root:
    python -m seq_interp.gui_app
or:
    python seq_interp/gui_app.py

Optionally pass a .seq file as the first CLI argument to pre-load it:
    python seq_interp/gui_app.py data/input/test1_full.seq
"""
from __future__ import annotations

import os
import sys

# Ensure the repo root is importable regardless of how the script is invoked.
_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 _path in (_repo_root, _lf_scanner_root):
    if _path not in sys.path:
        sys.path.insert(0, _path)

import argparse

from PySide6.QtWidgets import QApplication

from seq_interp.src.gui.main_window import MainWindow


def main() -> None:
    parser = argparse.ArgumentParser(
        description="MRI Sequence Interpreter - interactive GUI"
    )
    parser.add_argument(
        "seq_file", nargs="?", default=None,
        help="Optional .seq file to load on startup",
    )
    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",
    )
    args = parser.parse_args()

    app = QApplication(sys.argv)
    app.setApplicationName("MRI Sequence Interpreter")
    app.setOrganizationName("LF-MRI")
    # Qt6 / PySide6: high-DPI is enabled automatically - AA_UseHighDpiPixmaps
    # is deprecated and emits a DeprecationWarning; omit it.

    win = MainWindow()

    # Pre-load files from CLI arguments if given
    if args.hw_config and os.path.isfile(args.hw_config):
        win._hw_config_path = os.path.abspath(args.hw_config)
        win._log(f"HW config pre-loaded: {win._hw_config_path}")

    if args.output_dir:
        win._output_dir = os.path.abspath(args.output_dir)
        win._log(f"Output dir: {win._output_dir}")

    if args.seq_file and os.path.isfile(args.seq_file):
        win._seq_path = os.path.abspath(args.seq_file)
        name = os.path.basename(win._seq_path)
        win._set_seq_state("selected", name)
        win._btn_run.setEnabled(True)
        win._log(f"Sequence pre-loaded: {win._seq_path}")

    # 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()
