#!/bin/bash
set -u
API="http://127.0.0.1:8090"
cd /opt/projectcat-backend || exit 1
SECRET=$(grep -E "^SERVER_SECRET=" .env | cut -d= -f2-)
TS=$(date +%s)
j() { python3 -c "import sys,json;d=json.load(sys.stdin);print(d.get('$1',''))" 2>/dev/null; }
jn() { python3 -c "import sys,json;d=json.load(sys.stdin);print(json.dumps(d))" 2>/dev/null; }

reg() { curl -s -X POST "$API/auth/register" -H "Content-Type: application/json" -d "{\"email\":\"$1\",\"password\":\"Test1234!\"}" -o /dev/null; }
login() { curl -s -X POST "$API/auth/login" -H "Content-Type: application/json" -d "{\"email\":\"$1\",\"password\":\"Test1234!\"}" | j access_token; }
mkchar() { curl -s -X POST "$API/characters" -H "Authorization: Bearer $1" -H "Content-Type: application/json" -d "{\"name\":\"$2\",\"class_index\":0,\"spec_index\":0}" | j id; }

EA="pa_${TS}@bugforge.de"; EB="pb_${TS}@bugforge.de"
NA="Leader${TS}"; NB="Member${TS}"
reg "$EA"; reg "$EB"
TA=$(login "$EA"); TB=$(login "$EB")
CA=$(mkchar "$TA" "$NA"); CB=$(mkchar "$TB" "$NB")
echo "A(Leader) char=$CA   B(Member) char=$CB"

echo "== Beide betreten die Welt (Hub) =="
curl -s -X POST "$API/world/enter" -H "Authorization: Bearer $TA" -H "Content-Type: application/json" -d "{\"character_id\":$CA}" -o /dev/null -w "  A enter=%{http_code}\n"
curl -s -X POST "$API/world/enter" -H "Authorization: Bearer $TB" -H "Content-Type: application/json" -d "{\"character_id\":$CB}" -o /dev/null -w "  B enter=%{http_code}\n"

echo "== A gründet Gruppe =="
PID=$(curl -s -X POST "$API/party/create" -H "Authorization: Bearer $TA" -H "Content-Type: application/json" -d "{\"character_id\":$CA}" | python3 -c "import sys,json;print(json.load(sys.stdin)['party']['party_id'])")
echo "  party_id=$PID"

echo "== B sieht offene Gruppen + tritt bei =="
curl -s "$API/party/list" -H "Authorization: Bearer $TB"; echo
curl -s -X POST "$API/party/join" -H "Authorization: Bearer $TB" -H "Content-Type: application/json" -d "{\"character_id\":$CB,\"party_id\":$PID}" -o /dev/null -w "  B join=%{http_code}\n"

echo "== A sieht Gruppe (2 Mitglieder) =="
curl -s "$API/party" -H "Authorization: Bearer $TA" | jn; echo

echo "== A startet Instanz (Dungeon) =="
curl -s -X POST "$API/party/start-instance" -H "Authorization: Bearer $TA" -H "Content-Type: application/json" -d "{\"map_name\":\"/Game/Maps/Dungeon\",\"mode\":\"dungeon\"}" -o /dev/null -w "  start=%{http_code}\n"

echo "== Beide holen ihr Instanz-Ticket =="
TICKA=$(curl -s "$API/party" -H "Authorization: Bearer $TA" | python3 -c "import sys,json;d=json.load(sys.stdin);print((d['party'].get('instance') or {}).get('ticket',''))")
TICKB=$(curl -s "$API/party" -H "Authorization: Bearer $TB" | python3 -c "import sys,json;d=json.load(sys.stdin);print((d['party'].get('instance') or {}).get('ticket',''))")
PORTA=$(curl -s "$API/party" -H "Authorization: Bearer $TA" | python3 -c "import sys,json;d=json.load(sys.stdin);print((d['party'].get('instance') or {}).get('server_port',''))")
echo "  A-Ticket=${TICKA:0:10}...  B-Ticket=${TICKB:0:10}...  Port=$PORTA"
[ -n "$TICKA" ] && [ -n "$TICKB" ] && [ "$TICKA" != "$TICKB" ] && echo "  OK: zwei verschiedene gültige Tickets" || echo "  FEHLER bei Tickets"
docker ps --filter "name=projectcat-lobby-" --format "  Instanz: {{.Names}} {{.Status}} {{.Ports}}"

echo "== Tickets serverseitig verifizieren =="
curl -s -X POST "$API/internal/verify-ticket" -H "X-Server-Secret: $SECRET" -H "Content-Type: application/json" -d "{\"ticket\":\"$TICKA\"}" | jn; echo

echo "== W5: Freundschaft (B fragt A an, A nimmt an) =="
curl -s -X POST "$API/friends/request" -H "Authorization: Bearer $TB" -H "Content-Type: application/json" -d "{\"character_name\":\"$NA\"}" -o /dev/null -w "  B->A request=%{http_code}\n"
curl -s "$API/friends" -H "Authorization: Bearer $TA" | jn; echo "  (A incoming ^)"
curl -s -X POST "$API/friends/accept" -H "Authorization: Bearer $TA" -H "Content-Type: application/json" -d "{\"character_name\":\"$NB\"}" -o /dev/null -w "  A accept=%{http_code}\n"
curl -s "$API/friends" -H "Authorization: Bearer $TA" | jn; echo "  (A friends ^)"

echo "== Cleanup =="
curl -s -X POST "$API/party/end-instance" -H "Authorization: Bearer $TA" -o /dev/null -w "  end-instance=%{http_code}\n"
LID=$(docker ps --filter "name=projectcat-lobby-" --format "{{.Names}}" | head -1 | sed 's/projectcat-lobby-//')
[ -n "$LID" ] && curl -s -X POST "$API/internal/lobbies/$LID/end" -H "X-Server-Secret: $SECRET" -o /dev/null -w "  lobby-end=%{http_code}\n"
docker rm -f "projectcat-lobby-$LID" >/dev/null 2>&1
curl -s -X POST "$API/party/leave" -H "Authorization: Bearer $TA" -o /dev/null
curl -s -X POST "$API/party/leave" -H "Authorization: Bearer $TB" -o /dev/null
echo "== fertig =="
