from __future__ import annotations

import argparse
import sys

from .app import PveVmSetupApp
from .doctor import run_live_doctor
from .settings import AppSettings
from .terminal_compat import apply_runtime_compatibility


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Proxmox VM setup TUI")
    parser.add_argument(
        "--doctor-live",
        action="store_true",
        help="Run live Proxmox connectivity and authentication diagnostics.",
    )
    return parser


def main(argv: list[str] | None = None) -> int:
    parser = build_parser()
    args = parser.parse_args(argv)
    apply_runtime_compatibility()
    settings = AppSettings.from_env()

    if args.doctor_live:
        return run_live_doctor(settings, stream=sys.stdout)

    app = PveVmSetupApp(settings)
    app.run(mouse=False)
    return 0
