#!/bin/bash
# Add user2 to the group (user1 is owner)
source "$(dirname "$0")/config.sh"

TOKEN1=$(load_state "TOKEN1")
GROUP_ID=$(load_state "GROUP_ID")
USER2_ID=$(load_state "USER2_ID")

if [[ -z "$TOKEN1" || -z "$GROUP_ID" || -z "$USER2_ID" ]]; then
    echo "ERROR: Missing state. Run previous scripts first."
    echo "  TOKEN1=$TOKEN1"
    echo "  GROUP_ID=$GROUP_ID"
    echo "  USER2_ID=$USER2_ID"
    exit 1
fi

echo "=== Adding user2 (ID: $USER2_ID) to group $GROUP_ID ==="
RESP=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/mod/group/addclients" \
    -d "token=$TOKEN1" \
    -d "groupid=$GROUP_ID" \
    -d "clients=$USER2_ID")
BODY=$(echo "$RESP" | head -1)
CODE=$(echo "$RESP" | tail -1)
echo "Response: $BODY (HTTP $CODE)"
