#!/bin/bash
# Provision VWP extensions 101-118
# Domain: vwp.91912.service
# E911 Address: a-6a395c03d4cfe

DOMAIN="vwp.91912.service"
E911="a-6a395c03d4cfe"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PY_WRAPPER="bash $SCRIPT_DIR/../../../.claude/scripts/py.sh"

# User list: extension|first_name|last_name|email
USERS=(
  "101|Natalya||natalya@vwp.com"
  "102|Jesse||jesse@vwp.com"
  "103|Rose||rose@vwp.com"
  "104|Shelly||shelly@vwp.com"
  "105|J.R.||jr@vwp.com"
  "106|Tammy||tammy@vwp.com"
  "107|Payroll|Department|payroll@vwp.com"
  "108|Shannon||shannon@vwp.com"
  "109|Kayla||kayla@vwp.com"
  "110|Ron||ron@vwp.com"
  "111|Kitchen|Phone|kitchen@vwp.com"
  "112|Conference|Room|conferenceroom@vwp.com"
  "113|Chris||chris@vwp.com"
  "114|TY||ty@vwp.com"
  "115|Toni||toni@vwp.com"
  "116|Bart||bart@vwp.com"
  "117|Jesse|III|jesse3@vwp.com"
  "118|Warehouse|Phone|warehouse@vwp.com"
)

echo "[INFO] Creating 18 VWP users in PacketDial..."
echo ""

SUCCESS=0
FAILED=0

for user_data in "${USERS[@]}"; do
  IFS='|' read -r ext first last email <<< "$user_data"

  echo "[INFO] Creating extension $ext: $first $last"

  # Build JSON body (name-last-name is required even if empty)
  LAST_NAME="${last:-.}"
  BODY="{\"user\":\"$ext\",\"name-first-name\":\"$first\",\"name-last-name\":\"$LAST_NAME\",\"email\":\"$email\",\"emergency-address-id\":\"$E911\"}"

  # Create user
  RESULT=$($PY_WRAPPER "$SCRIPT_DIR/ns.py" create-user "$DOMAIN" --body "$BODY" --confirm 2>&1)

  if echo "$RESULT" | grep -q "HTTP 400.*already exists"; then
    echo "  [SKIP] Extension $ext already exists"
    ((SUCCESS++))
  elif echo "$RESULT" | grep -qE "HTTP [45]"; then
    echo "  [ERROR] Failed to create extension $ext: $RESULT"
    ((FAILED++))
  else
    echo "  [OK] Created extension $ext"
    ((SUCCESS++))
  fi
done

echo ""
echo "[SUMMARY] Users: $SUCCESS created/existing, $FAILED failed"
