#!/bin/bash
# Issue: demote_synonym_match appears to have no visible effect with text_match_type=sum_score
# Customer Reference: T-2816
# Typesense Version: 30.0
# Description:
#   Demonstrates that demote_synonym_match=true is applied, but the overall ranking can remain
#   unchanged in sum_score mode when other weighted fields dominate the aggregate score.

set -e

### Configuration ########################################

TYPESENSE_API_KEY=xyz
PORT=8108
TYPESENSE_HOST=http://localhost:${PORT}
CONTAINER_NAME=typesense-issue-T-2816-demote-synonym-sum-score
DATA_DIR=$(pwd)/typesense-data-${CONTAINER_NAME}
COLLECTION_NAME=demote_syn_sum_demo
SYNONYM_SET_NAME=demo_synonyms

### Cleanup ########################################

cleanup() {
  docker stop ${CONTAINER_NAME} > /dev/null 2>&1 || true
  docker rm ${CONTAINER_NAME} > /dev/null 2>&1 || true
  rm -rf "${DATA_DIR}" 2>/dev/null || true
}
trap cleanup EXIT
cleanup

### Setup Typesense ########################################

mkdir -p "${DATA_DIR}"

MSYS_NO_PATHCONV=1 docker run -d \
  --name ${CONTAINER_NAME} \
  -p ${PORT}:8108 \
  -v "${DATA_DIR}:/data" \
  typesense/typesense:30.0 \
  --data-dir /data \
  --api-key=${TYPESENSE_API_KEY} \
  --enable-cors > /dev/null

for i in $(seq 1 60); do
  curl -sf "${TYPESENSE_HOST}/health" > /dev/null 2>&1 && break
  sleep 1
done
curl -sf "${TYPESENSE_HOST}/health" > /dev/null 2>&1 || { echo "FATAL: Typesense failed to start"; exit 1; }

### Helpers ########################################

api() {
  curl -sSf -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" "$@"
}

wait_for_collection() {
  local coll="$1"
  for i in $(seq 1 30); do
    api "${TYPESENSE_HOST}/collections/${coll}" > /dev/null 2>&1 && return 0
    sleep 1
  done
  echo "FATAL: collection '${coll}' did not become available"
  exit 1
}

run_search() {
  local demote="$1"
  local query_by="$2"
  local query_by_weights="$3"
  local text_match_type="$4"

  local args=(
    -sSf -G
    -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
    --data-urlencode "q=cmo"
    --data-urlencode "query_by=${query_by}"
    --data-urlencode "text_match_type=${text_match_type}"
    --data-urlencode "demote_synonym_match=${demote}"
    --data-urlencode "per_page=10"
    --data-urlencode "sort_by=_text_match:desc"
    --data-urlencode "include_fields=id,title,notes"
    "${TYPESENSE_HOST}/collections/${COLLECTION_NAME}/documents/search"
  )

  if [ -n "${query_by_weights}" ]; then
    args+=(--data-urlencode "query_by_weights=${query_by_weights}")
  fi

  curl "${args[@]}"
}

print_hits() {
  local json="$1"
  echo "${json}" | jq -r '.hits | to_entries[] | "\(.key+1)\t\(.value.document.id)\t\(.value.text_match)\t\(.value.document.title)\t\(.value.document.notes)"'
}

score_for_id() {
  local json="$1"
  local id="$2"
  echo "${json}" | jq -r --arg id "${id}" '.hits[] | select(.document.id == $id) | .text_match'
}

### Create Synonym Set + Collection ########################################

api "${TYPESENSE_HOST}/synonym_sets/${SYNONYM_SET_NAME}" \
  -X PUT \
  -H "Content-Type: application/json" \
  -d '{
    "items": [
      {
        "id": "cmo-map",
        "synonyms": ["cmo", "chief marketing officer"]
      }
    ]
  }' > /dev/null

api "${TYPESENSE_HOST}/collections" -X POST -H "Content-Type: application/json" -d "{
  \"name\": \"${COLLECTION_NAME}\",
  \"fields\": [
    {\"name\": \"id\", \"type\": \"string\"},
    {\"name\": \"title\", \"type\": \"string\"},
    {\"name\": \"notes\", \"type\": \"string\"}
  ],
  \"synonym_sets\": [\"${SYNONYM_SET_NAME}\"]
}" > /dev/null

wait_for_collection "${COLLECTION_NAME}"

### Import Documents ########################################

cat > "${DATA_DIR}/docs.jsonl" <<'JSONL'
{"id":"exact_only","title":"cmo","notes":"board update"}
{"id":"synonym_involved","title":"chief marketing officer","notes":"chief marketing officer quarterly planning"}
JSONL

IMPORT_RESP=$(api "${TYPESENSE_HOST}/collections/${COLLECTION_NAME}/documents/import?action=create" \
  -X POST -H "Content-Type: text/plain" --data-binary @"${DATA_DIR}/docs.jsonl")
IMPORT_OK=$(echo "${IMPORT_RESP}" | jq -s -r '[.[] | select(.success == true)] | length')
[ "${IMPORT_OK}" -eq 2 ] || { echo "FATAL: import failed (success=${IMPORT_OK}/2)"; exit 1; }

### Reproduce ########################################

# Controlled case: title-only + max_score.
# Expected with demote=true: direct exact token should rank above synonym-expanded match.
CONTROL_ON=$(run_search "true" "title" "" "max_score")

# Sum-score case across fields.
# The synonym penalty is still applied on title, but notes (weight 15) dominates total score.
SUM_OFF=$(run_search "false" "title,notes" "1,15" "sum_score")
SUM_ON=$(run_search "true" "title,notes" "1,15" "sum_score")

echo "CONTROL (title only, max_score, demote=true)"
echo "pos	id	text_match	title	notes"
print_hits "${CONTROL_ON}"

echo
echo "SUM_SCORE (title,notes weights=1,15, demote=false)"
echo "pos	id	text_match	title	notes"
print_hits "${SUM_OFF}"

echo
echo "SUM_SCORE (title,notes weights=1,15, demote=true)"
echo "pos	id	text_match	title	notes"
print_hits "${SUM_ON}"

CONTROL_TOP_ON=$(echo "${CONTROL_ON}" | jq -r '.hits[0].document.id')
SUM_TOP_OFF=$(echo "${SUM_OFF}" | jq -r '.hits[0].document.id')
SUM_TOP_ON=$(echo "${SUM_ON}" | jq -r '.hits[0].document.id')
SUM_SYNONYM_OFF=$(score_for_id "${SUM_OFF}" "synonym_involved")
SUM_SYNONYM_ON=$(score_for_id "${SUM_ON}" "synonym_involved")

SUM_SYNONYM_DROPPED=false
if [ "${SUM_SYNONYM_OFF}" -gt "${SUM_SYNONYM_ON}" ]; then
  SUM_SYNONYM_DROPPED=true
fi

echo
echo "checks:"
echo "  control_top_with_demote=${CONTROL_TOP_ON}"
echo "  sum_top_without_demote=${SUM_TOP_OFF}"
echo "  sum_top_with_demote=${SUM_TOP_ON}"
echo "  synonym_involved_score_without_demote=${SUM_SYNONYM_OFF}"
echo "  synonym_involved_score_with_demote=${SUM_SYNONYM_ON}"
echo "  synonym_involved_score_dropped=${SUM_SYNONYM_DROPPED}"

if [ "${CONTROL_TOP_ON}" = "exact_only" ] && \
   [ "${SUM_TOP_OFF}" = "synonym_involved" ] && \
   [ "${SUM_TOP_ON}" = "synonym_involved" ] && \
   [ "${SUM_SYNONYM_DROPPED}" = "true" ]; then
  echo
  echo "RESULT: REPRODUCED"
  echo "demote_synonym_match is active (score drops), but in sum_score mode ranking can remain unchanged."
  exit 0
fi

echo
echo "RESULT: NOT REPRODUCED"
echo "Dataset/weights need adjustment for this environment."
exit 1
