#!/usr/bin/env bash
# nerf-az-boards-mywi-show -- Show details and comments for a work item assigned to you. Verifies that System.AssignedTo matches the current user; refuses items assigned to anyone else (use az-boards-wi-show for those).
# Generated from az-boards manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=none

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  echo "error: nerf-az-boards-mywi-show requires bash 4+. Found bash ${BASH_VERSION:-unknown}" >&2
  echo "  hint: on macOS, install a newer bash via 'brew install bash'" >&2
  exit 1
fi

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-az-boards-mywi-show [--project|-p <project>] <wi_id>

Options:
  --project, -p <project>
      Azure DevOps project name or ID (auto-detected from the git remote if omitted)

Arguments:
  <wi_id> (required)
      Work item ID (numeric, must be assigned to you)
      Must match: ^[0-9]+$

Show details and comments for a work item assigned to you. Verifies that System.AssignedTo matches the current user; refuses items assigned to anyone else (use az-boards-wi-show for those).
EOF
  exit 1
}

PROJECT=""
_PROJECT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --project|-p) if [[ -n "${_PROJECT_SET}" ]]; then echo "error: --project can only be specified once" >&2; exit 1; fi; PROJECT="$2"; _PROJECT_SET=true; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

_WI_ID_SET=""
if [[ $# -gt 0 ]]; then
  WI_ID="$1"
  _WI_ID_SET=true
  shift
else
  WI_ID=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-az-boards-mywi-show: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

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

if [[ -z "${WI_ID}" ]]; then
  echo "error: nerf-az-boards-mywi-show: missing required argument <wi_id>" >&2
  echo "  hint: provide a value for <wi_id>" >&2
  usage
fi

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

which jq > /dev/null 2>&1 || { echo 'error: nerf-az-boards-mywi-show: jq is required but not installed (e.g. apt-get install jq, brew install jq).' >&2; exit 1; }

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: nerf-az-boards-mywi-show would run inline script"
  exit 0
fi

CURRENT_USER=$(az devops invoke --area connectionData --resource connectionData --output json \
  | jq -r '.authenticatedUser.uniqueName // ""')
if [[ -z "${CURRENT_USER}" ]]; then
  echo "error: az-boards-mywi-show: could not determine current Azure DevOps user (try 'az login')" >&2
  exit 1
fi
ARGS=(az boards work-item show --id "${WI_ID}" --output json)
[[ -n "${PROJECT}" ]] && ARGS+=(--project "${PROJECT}")
WI_JSON=$("${ARGS[@]}") || {
  echo "error: az-boards-mywi-show: could not fetch work item ${WI_ID}" >&2
  exit 1
}
ASSIGNED=$(printf '%s' "${WI_JSON}" | jq -r '.fields["System.AssignedTo"].uniqueName // ""')
if [[ -z "${ASSIGNED}" ]]; then
  echo "error: az-boards-mywi-show: work item ${WI_ID} is not assigned to anyone" >&2
  exit 1
fi
ASSIGNED_LC=$(printf '%s' "${ASSIGNED}" | tr '[:upper:]' '[:lower:]')
CURRENT_USER_LC=$(printf '%s' "${CURRENT_USER}" | tr '[:upper:]' '[:lower:]')
if [[ "${ASSIGNED_LC}" != "${CURRENT_USER_LC}" ]]; then
  echo "error: az-boards-mywi-show: work item ${WI_ID} is assigned to ${ASSIGNED}, not you (${CURRENT_USER}). Use az-boards-wi-show for items assigned to others." >&2
  exit 1
fi
echo "${WI_JSON}"
