# shellcheck shell=bash

_okstra_ctl_list() {
  local project="all" task_group="all" status="all" since="" limit="0"
  local include_archive="false"
  while [[ $# -gt 0 ]]; do
    case "$1" in
      --project) project="$2"; shift 2 ;;
      --task-group) task_group="$2"; shift 2 ;;
      --status) status="$2"; shift 2 ;;
      --since) since="$2"; shift 2 ;;
      --limit) limit="$2"; shift 2 ;;
      --include-archive) include_archive="true"; shift ;;
      *) printf 'list: unknown option: %s\n' "$1" >&2; exit 2 ;;
    esac
  done
  OKSTRA_HOME_RESOLVED="$(okstra_central_home)" \
  OKSTRA_CTL_LIB_DIR="$SCRIPT_DIR" \
  OK_PROJECT="$project" OK_TG="$task_group" OK_STATUS="$status" \
  OK_SINCE="$since" OK_LIMIT="$limit" OK_ARC="$include_archive" \
  python3 - <<'PY'
import os, sys
sys.path.insert(0, os.environ["OKSTRA_CTL_LIB_DIR"])
from pathlib import Path
from okstra_ctl import list_runs, format_runs_table
rows = list_runs(
    Path(os.environ["OKSTRA_HOME_RESOLVED"]),
    project=os.environ["OK_PROJECT"], task_group=os.environ["OK_TG"],
    status=os.environ["OK_STATUS"], since=os.environ["OK_SINCE"],
    limit=int(os.environ["OK_LIMIT"]),
    include_archive=os.environ["OK_ARC"] == "true",
)
print(format_runs_table(rows), end="")
PY
}
