# Dashboard Runtime Smoke Test CURLs
# Purpose: quick re-test suite for queue-health dashboard endpoints.
# Auth note: TOKEN should be a JWT access token generated by /iam/authenticate.

# ----------------------------------------------------------------------------
# Setup
# ----------------------------------------------------------------------------
export BASE_URL="http://localhost:9000/api"
export TOKEN="<PASTE_JWT_HERE>"

# Optional: verify token works.
curl -s "$BASE_URL/iam/me" \
  -H "Authorization: Bearer $TOKEN" | jq

# ----------------------------------------------------------------------------
# 1) Dashboard Definition
# Expected: 200 + dashboard metadata in .data
# ----------------------------------------------------------------------------
curl -s "$BASE_URL/dashboard/solid-core/queue-health/definition" \
  -H "Authorization: Bearer $TOKEN" | jq

# ----------------------------------------------------------------------------
# 2) Dashboard Variable Options (dynamic/static)
# Expected: 200 + array in .data
# ----------------------------------------------------------------------------

# queueName (dynamic provider)
curl -s "$BASE_URL/dashboard/solid-core/queue-health/variable-options/queueName?limit=20&offset=0&query=" \
  -H "Authorization: Bearer $TOKEN" | jq

# messageBroker (dynamic provider)
curl -s "$BASE_URL/dashboard/solid-core/queue-health/variable-options/messageBroker?limit=20&offset=0&query=" \
  -H "Authorization: Bearer $TOKEN" | jq

# stage (static selection)
curl -s "$BASE_URL/dashboard/solid-core/queue-health/variable-options/stage" \
  -H "Authorization: Bearer $TOKEN" | jq

# ----------------------------------------------------------------------------
# 3) Single Widget Data Endpoints
# Expected: success envelope with .data.meta + .data.data
# ----------------------------------------------------------------------------

# KPI: total messages
curl -s -X POST "$BASE_URL/dashboard/solid-core/queue-health/widgets/kpi-total-messages/data" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "variables": {
      "date": { "preset": "last_7_days" }
    }
  }' | jq

# Line chart: messages over time
curl -s -X POST "$BASE_URL/dashboard/solid-core/queue-health/widgets/chart-messages-over-time/data" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "variables": {
      "date": { "preset": "last_30_days" },
      "stage": ["succeeded", "failed"]
    }
  }' | jq

# Table: recent failures
curl -s -X POST "$BASE_URL/dashboard/solid-core/queue-health/widgets/table-recent-failures/data" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "variables": {
      "date": { "preset": "last_30_days" }
    }
  }' | jq

# ----------------------------------------------------------------------------
# 4) Batch Widget Data (subset)
# Expected: .data.widgets[] with provider-backed results
# ----------------------------------------------------------------------------
curl -s -X POST "$BASE_URL/dashboard/solid-core/queue-health/data" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "widgetNames": [
      "kpi-total-messages",
      "kpi-failed-messages",
      "kpi-success-rate",
      "chart-stage-distribution"
    ],
    "variables": {
      "date": { "preset": "last_7_days" }
    }
  }' | jq

# ----------------------------------------------------------------------------
# 5) Batch Widget Data (all widgets)
# Expected: all configured widgets execute in one call
# ----------------------------------------------------------------------------
curl -s -X POST "$BASE_URL/dashboard/solid-core/queue-health/data" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "variables": {
      "date": { "preset": "last_30_days" }
    }
  }' | jq

# ----------------------------------------------------------------------------
# 6) Layout Endpoints
# Expected:
# - GET returns defaultLayout and optional userLayout
# - PUT stores user layout once dashboardUserLayout generated model is available
# ----------------------------------------------------------------------------

# Read layout
curl -s "$BASE_URL/dashboard/solid-core/queue-health/layout" \
  -H "Authorization: Bearer $TOKEN" | jq

# Save layout
curl -s -X PUT "$BASE_URL/dashboard/solid-core/queue-health/layout" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "layoutJson": {
      "engine": "gridstack",
      "columns": 12,
      "items": [
        { "widgetId": "kpi-total-messages", "x": 0, "y": 0, "w": 3, "h": 2 }
      ]
    }
  }' | jq

# ----------------------------------------------------------------------------
# 7) Useful validation snippets
# ----------------------------------------------------------------------------

# Show only provider names from all-widgets batch response
curl -s -X POST "$BASE_URL/dashboard/solid-core/queue-health/data" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"variables":{"date":{"preset":"last_30_days"}}}' | jq '.data.widgets[].meta.providerName'

# Show only recent failure record sample
curl -s -X POST "$BASE_URL/dashboard/solid-core/queue-health/widgets/table-recent-failures/data" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"variables":{"date":{"preset":"last_30_days"}}}' | jq '.data.data.records[0]'
