#!/usr/bin/env python3
"""Human-friendly wrapper for demo validation checks.

Use this script instead of demo_test_ladder.py for clearer mode names.
"""

from __future__ import annotations

import argparse
import os
from types import SimpleNamespace
from typing import Iterable

import demo_test_ladder as core


def _namespace_for_core(args: argparse.Namespace) -> SimpleNamespace:
    return SimpleNamespace(
        orchestrator_health=args.orchestrator_health_url,
        edge_health=args.edge_health_url,
        rosbridge_url=args.rosbridge_ws_url,
        timeout_sec=args.timeout_sec,
        skip_preflight=args.skip_preflight,
        pytest_args=args.pytest_args,
        with_live_llm=args.with_live_llm,
        live_llm_endpoint=args.llm_endpoint,
        live_llm_model=args.llm_model,
        live_llm_intent=args.llm_intent,
        live_llm_timeout_sec=args.llm_timeout_sec,
        live_llm_use_openai_sdk=args.llm_use_openai_sdk,
        live_llm_no_response_format=args.llm_no_response_format,
    )


def _run_modes_in_order(modes: Iterable[str], core_args: SimpleNamespace) -> int:
    for mode in modes:
        if mode == "preflight":
            rc = core.run_preflight(core_args)
        elif mode == "live-llm":
            rc = core.run_live_llm_smoke(core_args)
        else:
            rc = core.run_layer(mode, core_args.pytest_args)
        if rc != 0:
            return rc
    return 0


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Run demo validation checks (simple modes)")
    parser.add_argument(
        "--mode",
        choices=("preflight", "quick", "integration", "scenario", "llm", "full"),
        default="quick",
        help=(
            "preflight=connectivity only, "
            "quick=unit+component, "
            "integration=mock integration tests, "
            "scenario=3-scene flow test, "
            "llm=real orchestrator planner call, "
            "full=all checks in order"
        ),
    )
    parser.add_argument(
        "--orchestrator-health-url",
        default=os.getenv("ORCHESTRATOR_HEALTH_URL", "http://127.0.0.1:8000/health"),
    )
    parser.add_argument(
        "--edge-health-url",
        default=os.getenv("EDGE_PROXY_HEALTH_URL", "http://127.0.0.1:8080/health"),
    )
    parser.add_argument(
        "--rosbridge-ws-url",
        default=os.getenv("ROSBRIDGE_URL", "wss://127.0.0.1:9090"),
    )
    parser.add_argument("--timeout-sec", type=float, default=2.0)
    parser.add_argument(
        "--skip-preflight",
        action="store_true",
        help="Skip connectivity checks for local/offline test runs",
    )
    parser.add_argument(
        "--with-live-llm",
        action="store_true",
        help="In full mode, include real planner call against LLM endpoint",
    )
    parser.add_argument(
        "--llm-endpoint",
        default=os.getenv(
            "LIVE_LLM_ENDPOINT",
            os.getenv("ORCHESTRATOR_LLM_ENDPOINT", "https://modelapi.klass.dev/v1/chat/completions"),
        ),
    )
    parser.add_argument(
        "--llm-model",
        default=os.getenv("LIVE_LLM_MODEL", os.getenv("ORCHESTRATOR_LLM_MODEL", "Qwen3-Next-80B-A3B-FP8")),
    )
    parser.add_argument(
        "--llm-intent",
        default="Navigate to office_scene, scan area, verify chemical container.",
    )
    parser.add_argument("--llm-timeout-sec", type=int, default=45)
    parser.add_argument("--llm-use-openai-sdk", action="store_true")
    parser.add_argument("--llm-no-response-format", action="store_true")
    parser.add_argument(
        "pytest_args",
        nargs=argparse.REMAINDER,
        help="Extra args forwarded to pytest (prefix with --)",
    )
    return parser.parse_args()


def main() -> int:
    args = parse_args()
    core_args = _namespace_for_core(args)

    if args.mode == "preflight":
        return core.run_preflight(core_args)

    if args.mode == "quick":
        if not args.skip_preflight:
            rc = core.run_preflight(core_args)
            if rc != 0:
                return rc
        return _run_modes_in_order(("unit", "component"), core_args)

    if args.mode == "integration":
        return core.run_layer("integration-mock", core_args.pytest_args)

    if args.mode == "scenario":
        if not args.skip_preflight:
            rc = core.run_preflight(core_args)
            if rc != 0:
                return rc
        return core.run_layer("scenario", core_args.pytest_args)

    if args.mode == "llm":
        return core.run_live_llm_smoke(core_args)

    # full
    if not args.skip_preflight:
        rc = core.run_preflight(core_args)
        if rc != 0:
            return rc
    ordered_modes = ["unit", "component", "integration-mock", "scenario"]
    if args.with_live_llm:
        ordered_modes.insert(3, "live-llm")
    return _run_modes_in_order(ordered_modes, core_args)


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