#!/usr/bin/env bash

set -euo pipefail

API_BASE_URL="${API_BASE_URL:-}"
SITE_ID="${SITE_ID:-}"
SITE_KEY="${SITE_KEY:-}"
EXPERIMENT_ID="${EXPERIMENT_ID:-}"
EXPERIMENT_KEY="${EXPERIMENT_KEY:-}"
VARIANT_ID="${VARIANT_ID:-}"
VARIANT_KEY="${VARIANT_KEY:-}"
VISITOR_ID="${VISITOR_ID:-visitor_001}"
PAGE_URL="${PAGE_URL:-}"
ASSIGNMENT_SALT="${ASSIGNMENT_SALT:-runtime-smoke}"
RELEASE_ID="${RELEASE_ID:-}"
RELEASE_VERSION="${RELEASE_VERSION:-1}"

if [[ -z "${API_BASE_URL}" ]]; then
  echo "Missing API_BASE_URL"
  exit 1
fi

if [[ -z "${SITE_ID}" && -z "${SITE_KEY}" ]]; then
  echo "Missing SITE_ID or SITE_KEY"
  exit 1
fi

if [[ -z "${PAGE_URL}" ]]; then
  echo "Missing PAGE_URL"
  exit 1
fi

if [[ -z "${EXPERIMENT_ID}" || -z "${EXPERIMENT_KEY}" ]]; then
  echo "Missing EXPERIMENT_ID or EXPERIMENT_KEY"
  exit 1
fi

if [[ -z "${VARIANT_ID}" || -z "${VARIANT_KEY}" ]]; then
  echo "Missing VARIANT_ID or VARIANT_KEY"
  exit 1
fi

CONTENT_HEADER="Content-Type: application/json"

EXPERIMENT_JSON="$(cat <<EOF
{
  "experiment_id": "${EXPERIMENT_ID}",
  "experiment_key": "${EXPERIMENT_KEY}",
  "status": "running",
  "site_key": "${SITE_KEY}",
  "assignment_salt": "${ASSIGNMENT_SALT}",
  "release_id": "${RELEASE_ID}",
  "release_version": ${RELEASE_VERSION},
  "payload": {
    "operations": [
      {
        "selector_type": "css",
        "selector_value": "h1",
        "action": "replace_text",
        "payload": {
          "text": "Smoke Test Runtime Heading"
        }
      }
    ]
  },
  "variants": [
    {
      "id": "${VARIANT_ID}",
      "variant_key": "${VARIANT_KEY}",
      "traffic_weight": 50,
      "is_control": false
    },
    {
      "id": "${VARIANT_ID}_control",
      "variant_key": "control",
      "traffic_weight": 50,
      "is_control": true
    }
  ]
}
EOF
)"

echo ""
echo "== Runtime Bootstrap =="
curl --silent --show-error --fail \
  -X POST \
  -H "${CONTENT_HEADER}" \
  -d "{
    \"site_id\": \"${SITE_ID}\",
    \"site_key\": \"${SITE_KEY}\",
    \"url\": \"${PAGE_URL}\",
    \"visitor_id\": \"${VISITOR_ID}\",
    \"user_agent\": \"Mozilla/5.0\"
  }" \
  "${API_BASE_URL}/api/runtime/bootstrap"

echo ""
echo ""
echo "== Runtime Assign =="
curl --silent --show-error --fail \
  -X POST \
  -H "${CONTENT_HEADER}" \
  -d "{
    \"visitor_id\": \"${VISITOR_ID}\",
    \"experiment\": ${EXPERIMENT_JSON}
  }" \
  "${API_BASE_URL}/api/runtime/assign"

echo ""
echo ""
echo "== Runtime Payload =="
curl --silent --show-error --fail \
  -X POST \
  -H "${CONTENT_HEADER}" \
  -d "{
    \"visitor_id\": \"${VISITOR_ID}\",
    \"experiment\": ${EXPERIMENT_JSON}
  }" \
  "${API_BASE_URL}/api/runtime/payload"

echo ""
echo ""
echo "== Runtime Impression Event =="
curl --silent --show-error --fail \
  -X POST \
  -H "${CONTENT_HEADER}" \
  -d "{
    \"site_id\": \"${SITE_ID}\",
    \"site_key\": \"${SITE_KEY}\",
    \"experiment_id\": \"${EXPERIMENT_ID}\",
    \"experiment_key\": \"${EXPERIMENT_KEY}\",
    \"variant_id\": \"${VARIANT_ID}\",
    \"variant_key\": \"${VARIANT_KEY}\",
    \"visitor_id\": \"${VISITOR_ID}\",
    \"event_name\": \"experiment_impression\",
    \"payload\": {
      \"page\": \"${PAGE_URL}\"
    }
  }" \
  "${API_BASE_URL}/api/runtime/events/impression"

echo ""
echo ""
echo "== Runtime Conversion Event =="
curl --silent --show-error --fail \
  -X POST \
  -H "${CONTENT_HEADER}" \
  -d "{
    \"site_id\": \"${SITE_ID}\",
    \"site_key\": \"${SITE_KEY}\",
    \"experiment_id\": \"${EXPERIMENT_ID}\",
    \"experiment_key\": \"${EXPERIMENT_KEY}\",
    \"variant_id\": \"${VARIANT_ID}\",
    \"variant_key\": \"${VARIANT_KEY}\",
    \"visitor_id\": \"${VISITOR_ID}\",
    \"event_name\": \"experiment_conversion\",
    \"payload\": {
      \"page\": \"${PAGE_URL}\"
    }
  }" \
  "${API_BASE_URL}/api/runtime/events/conversion"

echo ""
echo ""
echo "Runtime smoke test completed."
