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

BASE="http://localhost:8000/import/wiki"

echo "? 1) Health-Check"
curl -s -o /dev/null -w '%{http_code}\n' "${BASE}/health"

echo -e "\n? 2) Login"
curl -s -X POST "${BASE}/login" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"${WIKI_BOT_USER}\",\"password\":\"${WIKI_BOT_PASSWORD}\"}" | jq

echo -e "\n? 3) SMW-Ask: Alle Übungen abfragen und Titel ausgeben"
curl -s -G "${BASE}/semantic/pages" \
  --data-urlencode "category=Übungen" \
| jq -r 'keys[]' 

COUNT=$(curl -s -G "${BASE}/semantic/pages" \
  --data-urlencode "category=Übungen" \
| jq 'keys | length')
echo -e "\n? Anzahl Übungen: ${COUNT}"

echo -e "\n? 4) Details der ersten Übung"
FIRST_TITLE=$(curl -s -G "${BASE}/semantic/pages" \
  --data-urlencode "category=Übungen" \
| jq -r 'keys[0]')
echo "Erste Übung: ${FIRST_TITLE}"

FIRST_PAGEID=$(curl -s -G "${BASE}/semantic/pages" \
  --data-urlencode "category=Übungen" \
| jq -r ".\"${FIRST_TITLE}\".pageid")
echo "PageID: ${FIRST_PAGEID}"

echo -e "\n? 5) Wikitext der ersten Übung"
curl -s -G "${BASE}/pagecontent" \
  --data-urlencode "pageid=${FIRST_PAGEID}" \
  --data-urlencode "title=${FIRST_TITLE}" \
| jq '.content'

echo -e "\n? Test abgeschlossen."
