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

BASE_URL="http://127.0.0.1:8000"
COL="filter_test"

# Prüfe, ob jq installiert ist
if ! command -v jq &>/dev/null; then
  echo "Bitte installieren: sudo apt-get install -y jq"
  exit 1
fi

# Funktion zum Einfügen eines vollständigen Chunks
function test_embed_full {
  local SRC=$1; local OWNER=$2; local CAT=$3; local TEXT=$4
  local IDX=$5
  payload=$(jq -n \
    --arg col "$COL" \
    --arg txt "$TEXT" \
    --arg src "$SRC" \
    --arg owner "$OWNER" \
    --arg cat "$CAT" \
    --argjson idx $IDX '{
      collection: $col,
      chunks: [
        {
          text: $txt,
          source: $src,
          source_type: "unit-test",
          title: $txt,
          version: "v1",
          related_to: "rel",
          tags: ["t1","t2"],
          owner: $owner,
          context_tag: "ctx",
          imported_at: "2025-08-06T00:00:00Z",
          chunk_index: $idx,
          category: $cat
        }
      ]
    }')
  r=$(curl -s -X POST "${BASE_URL}/embed" -H "Content-Type: application/json" -d "$payload")
  cnt=$(echo "$r" | jq -r '.count // 0')
  if [[ "$cnt" -ne 1 ]]; then echo "/embed fehlgeschlagen: $r"; exit 1; fi
  echo "? Embed idx=$IDX src=$SRC owner=$OWNER cat=$CAT text=$TEXT"
}

# Funktion zum Löschenstest
function assert_delete {
  local DESC=$1; shift
  local PARAMS=$1; shift
  local EXPECT=$1; shift
  echo -n "Test: ${DESC} ... "
  local resp=$(curl -s -X DELETE "${BASE_URL}/delete-source?collection=${COL}&${PARAMS}")
  local cnt=$(echo "$resp" | jq -r '.count // 0')
  if [[ "$cnt" -eq "$EXPECT" ]]; then
    echo "? gelöscht=${cnt}"
  else
    echo "? gelöscht=${cnt}, erwartet=${EXPECT}"
    echo "  Response: $resp"
    exit 1
  fi
}

# Pause-Funktion
function pause {

  read -p "Drücke [Enter] um fortzufahren ..."
}

# 1) Initialisiere Collection
echo "=== 1) Initialisiere Test-Collection ==="
curl -s -X DELETE "${BASE_URL}/delete-collection?collection=${COL}" || true
pause

# 2) Einfügen von 4 Punkten
echo "=== 2) Einfügen von Testdaten ==="
test_embed_full s1 o1 c1 A 0
test_embed_full s2 o2 c2 B 1
test_embed_full s1 o2 c1 C 2
test_embed_full s2 o1 c2 D 3
pause

# 3) Einzelkriterien (jeder Test auf frischer Datenbasis)
echo "=== 3) Einzelkriterien ==="
for test in \
  "source=s1" "source=s2" "owner=o1" "owner=o2" "category=c1" "category=c2"; do
  # Reset Collection
  curl -s -X DELETE "${BASE_URL}/delete-collection?collection=${COL}" || true
  # Neu befüllen
  test_embed_full s1 o1 c1 A 0
  test_embed_full s2 o2 c2 B 1
  test_embed_full s1 o2 c1 C 2
  test_embed_full s2 o1 c2 D 3
  # Param und Expected bestimmen
  KEY=${test%%=*}
  VALUE=${test#*=}
  EXPECT=2
  echo -n "Test: ${test} ... "
  resp=$(curl -s -X DELETE "${BASE_URL}/delete-source?collection=${COL}&${test}")
  cnt=$(echo "$resp" | jq -r '.count // 0')
  if [[ "$cnt" -eq "$EXPECT" ]]; then
    echo "? gelöscht=${cnt}"
  else
    echo "? gelöscht=${cnt}, erwartet=${EXPECT}"; echo "  Response: $resp"; exit 1
  fi
  pause
done

# 4) Zwei Kriterien
echo "=== 4) Zwei Kriterien ==="
for params in \
  "source=s1&owner=o2" "source=s2&category=c2" "owner=o1&category=c2"; do
  curl -s -X DELETE "${BASE_URL}/delete-collection?collection=${COL}" || true
  test_embed_full s1 o1 c1 A 0
  test_embed_full s2 o2 c2 B 1
  test_embed_full s1 o2 c1 C 2
  test_embed_full s2 o1 c2 D 3
  assert_delete "$params" "$params" 1
  pause
done

# 5) Drei Kriterien
echo "=== 5) Drei Kriterien ==="
for params in "source=s1&owner=o2&category=c1"; do
  curl -s -X DELETE "${BASE_URL}/delete-collection?collection=${COL}" || true
  test_embed_full s1 o1 c1 A 0
  test_embed_full s2 o2 c2 B 1
  test_embed_full s1 o2 c1 C 2
  test_embed_full s2 o1 c2 D 3
  assert_delete "$params" "$params" 1
  pause
done

# 6) Kein Filter (400 erwartet)
echo "=== 4) Zwei Kriterien ==="
curl -s -X DELETE "${BASE_URL}/delete-collection?collection=${COL}" || true
# Neu befüllen
test_embed_full s1 o1 c1 A 0
test_embed_full s2 o2 c2 B 1
test_embed_full s1 o2 c1 C 2
test_embed_full s2 o1 c2 D 3
pause
assert_delete "s1+o2"           "source=s1&owner=o2"        1
assert_delete "s2+c2"           "source=s2&category=c2"     1
assert_delete "o1+c2"           "owner=o1&category=c2"      1
pause

# 5) Drei Kriterien
echo "=== 5) Drei Kriterien ==="
curl -s -X DELETE "${BASE_URL}/delete-collection?collection=${COL}" || true
# Neu befüllen
test_embed_full s1 o1 c1 A 0
test_embed_full s2 o2 c2 B 1
test_embed_full s1 o2 c1 C 2
test_embed_full s2 o1 c2 D 3
pause
assert_delete "s1+o2+c1"        "source=s1&owner=o2&category=c1" 1
pause

# 6) Kein Filter (400 erwartet)
echo "=== 6) Kein Filter (400 erwartet) ==="
echo -n "Test: no-filter ... "
local code=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE "${BASE_URL}/delete-source?collection=${COL}")
if [[ "$code" -eq 400 ]]; then echo "? HTTP 400"; else echo "? HTTP ${code}"; exit 1; fi
pause

# 7) Cleanup
echo "=== 7) Cleanup ==="
curl -s -X DELETE "${BASE_URL}/delete-collection?collection=${COL}" || true
echo "? Collection gelöscht"

echo "?? Test abgeschlossen! ??"
