#!/usr/bin/env bash
set -euo pipefail

SHUFFLE_BASE_URL="${SHUFFLE_BASE_URL:-http://localhost:5001}"
SHUFFLE_API_KEY="${SHUFFLE_API_KEY:-}"

if [[ -z "${SHUFFLE_API_KEY}" ]]; then
  echo "error: SHUFFLE_API_KEY is required"
  echo "example: SHUFFLE_API_KEY=xxxx scripts/create-shuffle-mvp-workflows.sh"
  exit 1
fi

create_workflow() {
  local name="$1"
  local description="$2"

  local payload
  payload=$(cat <<JSON
{
  "name": "${name}",
  "description": "${description}"
}
JSON
)

  local response
  response=$(curl -sS -X POST "${SHUFFLE_BASE_URL}/api/v1/workflows" \
    -H "Authorization: Bearer ${SHUFFLE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d "${payload}")

  WORKFLOW_RESPONSE="${response}" python3 - <<'PY'
import json, os, sys
raw = os.environ.get("WORKFLOW_RESPONSE", "")
try:
    data = json.loads(raw)
except Exception:
    print("error: invalid response")
    print(raw[:400])
    sys.exit(1)

if isinstance(data, dict) and data.get("success") is False:
    print("error: workflow create failed")
    print(raw)
    sys.exit(1)

print(f"created: {data.get('name')} id={data.get('id')}")
PY
}

create_workflow \
  "MVP - IOC Enrichment and Case Routing" \
  "Proposal-aligned MVP workflow: IOC enrichment, confidence scoring, callback to soc-integrator, incident routing to IRIS/PagerDuty stub."

create_workflow \
  "MVP - VPN Geo Anomaly Triage" \
  "Proposal-aligned MVP workflow: detect successful VPN auth from outside TH, enrich context, risk branch, callback for incident handling."
