#!/usr/bin/env bash
# nerf-az-boards-wi-create -- Create a new work item. Requires a type and title. Optionally set assigned-to, area, iteration, description, and a discussion comment. Returns the created work item as JSON.
# 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-create [--assigned-to <assigned_to>] [--area <area>] [--iteration <iteration>] [--description|-d <description>] [--discussion <discussion>] [--fields|-f <fields>] <type> <title>

Options:
  --assigned-to <assigned_to>
      Person to assign to (e.g. user@example.com)
  --area <area>
      Area path (e.g. MyProject\MyArea)
  --iteration <iteration>
      Iteration path (e.g. MyProject\Sprint 1)
  --description, -d <description>
      Description of the work item
  --discussion <discussion>
      Comment to add to the work item discussion
  --fields, -f <fields>
      Custom field assignment (e.g. "System.Tags=Rock")

Arguments:
  <type> (required)
      Work item type (e.g. Bug, Task, User Story, Feature)
  <title> (required)
      Title of the work item

Maps to: az boards work-item create --type <type> --title <title> <assigned_to> <area> <iteration> <description> <discussion> <fields> --output json

Create a new work item. Requires a type and title. Optionally set assigned-to, area, iteration, description, and a discussion comment. Returns the created work item as JSON..
EOF
  exit 1
}

ASSIGNED_TO=""
AREA=""
ITERATION=""
DESCRIPTION=""
DISCUSSION=""
FIELDS=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --assigned-to) if [[ -n "${ASSIGNED_TO}" ]]; then echo "error: --assigned-to can only be specified once" >&2; exit 1; fi; ASSIGNED_TO="$2"; shift 2 ;;
    --area) if [[ -n "${AREA}" ]]; then echo "error: --area can only be specified once" >&2; exit 1; fi; AREA="$2"; shift 2 ;;
    --iteration) if [[ -n "${ITERATION}" ]]; then echo "error: --iteration can only be specified once" >&2; exit 1; fi; ITERATION="$2"; shift 2 ;;
    --description|-d) if [[ -n "${DESCRIPTION}" ]]; then echo "error: --description can only be specified once" >&2; exit 1; fi; DESCRIPTION="$2"; shift 2 ;;
    --discussion) if [[ -n "${DISCUSSION}" ]]; then echo "error: --discussion can only be specified once" >&2; exit 1; fi; DISCUSSION="$2"; shift 2 ;;
    --fields|-f) if [[ -n "${FIELDS}" ]]; then echo "error: --fields can only be specified once" >&2; exit 1; fi; FIELDS="$2"; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

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

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

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

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

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

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: az boards work-item create --type "${TYPE}" --title "${TITLE}" ${ASSIGNED_TO:+"--assigned-to"} ${ASSIGNED_TO:+"$ASSIGNED_TO"} ${AREA:+"--area"} ${AREA:+"$AREA"} ${ITERATION:+"--iteration"} ${ITERATION:+"$ITERATION"} ${DESCRIPTION:+"--description"} ${DESCRIPTION:+"$DESCRIPTION"} ${DISCUSSION:+"--discussion"} ${DISCUSSION:+"$DISCUSSION"} ${FIELDS:+"--fields"} ${FIELDS:+"$FIELDS"} --output json"
  exit 0
fi

exec az boards work-item create --type "${TYPE}" --title "${TITLE}" ${ASSIGNED_TO:+"--assigned-to"} ${ASSIGNED_TO:+"$ASSIGNED_TO"} ${AREA:+"--area"} ${AREA:+"$AREA"} ${ITERATION:+"--iteration"} ${ITERATION:+"$ITERATION"} ${DESCRIPTION:+"--description"} ${DESCRIPTION:+"$DESCRIPTION"} ${DISCUSSION:+"--discussion"} ${DISCUSSION:+"$DISCUSSION"} ${FIELDS:+"--fields"} ${FIELDS:+"$FIELDS"} --output json
