#!/bin/bash
# Add options to custom select fields
# These are the newly created fields from the API setup

export JIRA_TOKEN="ATATT3xFfGF0ABlJi4MORJ1Mlyb6ZhR-I984TYCrUhoGH3yHf0bjhZKkJ_pk27s6mrb-GVoVtr0bXmXWtYDyQt0QL0Ut6BNpmfrz1ATJnIbqAsWVuWeKHxqyKJ8gZVApsi8OBc-jC2BVoG9TUEBDPDMWmG_0JG3zFU6bv8jTmGecwe2xlZzTiJo=E0093140"
export JIRA_EMAIL="v.samoilenko@ai-impress.com"
export JIRA_AUTH=$(echo -n "${JIRA_EMAIL}:${JIRA_TOKEN}" | base64)
export JIRA_URL="https://ai-impress.atlassian.net"

echo "============================================"
echo "Adding Options to Select Fields"
echo "============================================"
echo ""

add_options() {
  local field_id="$1"
  local field_name="$2"
  local options_json="$3"

  if [ -z "$field_id" ] || [ "$field_id" == "null" ]; then
    echo "⚠️  Skipping $field_name - field not found"
    return
  fi

  echo "Processing: $field_name ($field_id)"

  # Get context ID
  CONTEXT=$(curl -s -X GET \
    "${JIRA_URL}/rest/api/3/field/${field_id}/context" \
    -H "Authorization: Basic ${JIRA_AUTH}" \
    -H "Content-Type: application/json")

  CONTEXT_ID=$(echo "$CONTEXT" | jq -r '.values[0].id // empty')

  if [ -z "$CONTEXT_ID" ]; then
    echo "  ⚠️  No context found for $field_name"
    return
  fi

  echo "  Context ID: $CONTEXT_ID"

  # Add options
  RESULT=$(curl -s -X POST \
    "${JIRA_URL}/rest/api/3/field/${field_id}/context/${CONTEXT_ID}/option" \
    -H "Authorization: Basic ${JIRA_AUTH}" \
    -H "Content-Type: application/json" \
    -d "$options_json")

  if echo "$RESULT" | jq -e '.options' > /dev/null 2>&1; then
    COUNT=$(echo "$RESULT" | jq '.options | length')
    echo "  ✓ Added $COUNT options"
  else
    if echo "$RESULT" | jq -e '.errorMessages' > /dev/null 2>&1; then
      ERROR=$(echo "$RESULT" | jq -r '.errorMessages[0]')
      echo "  ❌ Error: $ERROR"
    else
      echo "  ⚠️  Unexpected response"
    fi
  fi
  echo ""
  sleep 1
}

# Lead Source (customfield_10110)
add_options "customfield_10110" "Lead Source" '{
  "options": [
    {"value": "Website", "disabled": false},
    {"value": "Referral", "disabled": false},
    {"value": "LinkedIn", "disabled": false},
    {"value": "Webinar", "disabled": false},
    {"value": "Cold Outreach", "disabled": false},
    {"value": "Partner", "disabled": false},
    {"value": "Google Ads", "disabled": false},
    {"value": "Social Media", "disabled": false},
    {"value": "Other", "disabled": false}
  ]
}'

# Lead Status (customfield_10111)
add_options "customfield_10111" "Lead Status" '{
  "options": [
    {"value": "New", "disabled": false},
    {"value": "Contacted", "disabled": false},
    {"value": "Qualified", "disabled": false},
    {"value": "Proposal Sent", "disabled": false},
    {"value": "Negotiation", "disabled": false},
    {"value": "Won", "disabled": false},
    {"value": "Lost", "disabled": false},
    {"value": "On Hold", "disabled": false}
  ]
}'

# Service Package (customfield_10112)
add_options "customfield_10112" "Service Package" '{
  "options": [
    {"value": "Starter (£450-850)", "disabled": false},
    {"value": "Growth (£950-1900)", "disabled": false},
    {"value": "Pro (£2400-6000)", "disabled": false},
    {"value": "Enterprise (£10K+)", "disabled": false},
    {"value": "Custom", "disabled": false}
  ]
}'

# Invoice Status (customfield_10113)
add_options "customfield_10113" "Invoice Status" '{
  "options": [
    {"value": "Draft", "disabled": false},
    {"value": "Sent", "disabled": false},
    {"value": "Viewed", "disabled": false},
    {"value": "Partial Payment", "disabled": false},
    {"value": "Paid", "disabled": false},
    {"value": "Overdue", "disabled": false},
    {"value": "Cancelled", "disabled": false}
  ]
}'

# Contract Type (customfield_10114)
add_options "customfield_10114" "Contract Type" '{
  "options": [
    {"value": "Service Agreement", "disabled": false},
    {"value": "NDA", "disabled": false},
    {"value": "Maintenance Contract", "disabled": false},
    {"value": "Partnership Agreement", "disabled": false},
    {"value": "Subcontractor Agreement", "disabled": false},
    {"value": "Other", "disabled": false}
  ]
}'

# Ticket Category (customfield_10115)
add_options "customfield_10115" "Ticket Category" '{
  "options": [
    {"value": "Bug Report", "disabled": false},
    {"value": "Feature Request", "disabled": false},
    {"value": "How-To Question", "disabled": false},
    {"value": "Integration Issue", "disabled": false},
    {"value": "Performance Issue", "disabled": false},
    {"value": "Billing Question", "disabled": false},
    {"value": "Other", "disabled": false}
  ]
}'

# SLA Priority (customfield_10116)
add_options "customfield_10116" "SLA Priority" '{
  "options": [
    {"value": "Critical (2h response)", "disabled": false},
    {"value": "High (4h response)", "disabled": false},
    {"value": "Medium (8h response)", "disabled": false},
    {"value": "Low (24h response)", "disabled": false}
  ]
}'

# Environment (customfield_10117)
add_options "customfield_10117" "Environment" '{
  "options": [
    {"value": "Development", "disabled": false},
    {"value": "Staging", "disabled": false},
    {"value": "Production", "disabled": false}
  ]
}'

echo "============================================"
echo "✅ All options added successfully!"
echo "============================================"
