#!/usr/bin/env bash
# nerf-az-boards-wi-add-parent -- Set the parent of a work item. Takes the child ID and parent ID as positional arguments. Idempotent if the link already exists.
# Generated from az-boards manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=remote

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-az-boards-wi-add-parent <child_id> <parent_id>

Arguments:
  <child_id> (required)
      Work item ID of the child (numeric)
      Must match: ^[0-9]+$
  <parent_id> (required)
      Work item ID of the parent (numeric)
      Must match: ^[0-9]+$

Maps to: az boards work-item relation add --id <child_id> --relation-type parent --target-id <parent_id> --output json

Set the parent of a work item. Takes the child ID and parent ID as positional arguments. Idempotent if the link already exists..
EOF
  exit 1
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

CHILD_ID="${1:-}"
shift 2>/dev/null || true
PARENT_ID="${1:-}"
shift 2>/dev/null || true

if [[ -n "${CHILD_ID}" ]] && [[ "${CHILD_ID}" == -* ]]; then
  echo "error: nerf-az-boards-wi-add-parent: <child_id> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${CHILD_ID}" ]]; then
  echo "error: nerf-az-boards-wi-add-parent: missing required argument <child_id>" >&2
  echo "  hint: provide a value for <child_id>" >&2
  usage
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${CHILD_ID}" ]] && ! [[ "${CHILD_ID}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-az-boards-wi-add-parent: argument <child_id> does not match required pattern" >&2
  echo "  value:   \"${CHILD_ID}\"" >&2
  echo "  pattern: ^[0-9]+$" >&2
  echo "  hint: value must match ^[0-9]+$" >&2
  exit 1
fi

if [[ -n "${PARENT_ID}" ]] && [[ "${PARENT_ID}" == -* ]]; then
  echo "error: nerf-az-boards-wi-add-parent: <parent_id> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${PARENT_ID}" ]]; then
  echo "error: nerf-az-boards-wi-add-parent: missing required argument <parent_id>" >&2
  echo "  hint: provide a value for <parent_id>" >&2
  usage
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${PARENT_ID}" ]] && ! [[ "${PARENT_ID}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-az-boards-wi-add-parent: argument <parent_id> does not match required pattern" >&2
  echo "  value:   \"${PARENT_ID}\"" >&2
  echo "  pattern: ^[0-9]+$" >&2
  echo "  hint: value must match ^[0-9]+$" >&2
  exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: az boards work-item relation add --id "${CHILD_ID}" --relation-type parent --target-id "${PARENT_ID}" --output json"
  exit 0
fi

exec az boards work-item relation add --id "${CHILD_ID}" --relation-type parent --target-id "${PARENT_ID}" --output json
