from __future__ import annotations

import argparse
import sys
from pathlib import Path


def _print_missing_pyside6(exc: ModuleNotFoundError) -> int:
    base_dir = Path(__file__).resolve().parent
    requirements = base_dir / "requirements.txt"
    print("PySide6 is not installed.")
    print(f"Install it with: python -m pip install -r {requirements}")
    print(f"Original error: {exc}")
    return 2


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description="YunTools Qt Widgets preview")
    parser.add_argument("--self-test", action="store_true", help="Import-check the Qt preview without opening a window.")
    args = parser.parse_args(argv)

    try:
        from qt_multicloud.app import run_app, self_test
    except ModuleNotFoundError as exc:
        if exc.name == "PySide6":
            return _print_missing_pyside6(exc)
        raise

    if args.self_test:
        return self_test()
    return run_app()


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