echo "#!/bin/bash"

. ./test_shared_functions.sh

extract_document_id() {
  R=$1
  C=$(echo $R | sed 's/[0-9][0-9][0-9]$//')
  #echo ">>${C}<<"
  if [[ ! -z "$C" ]] && [[ "$C" == *results* ]]; then
    COLLECTION_IDS=($(echo $C | jq -r '.results[] | select(.document_count >= 1) | .id' | tr '\n' ' '))
    echo "CIDs = ${COLLECTION_IDS[*]}"
  else
    echo "No IDs detected in the response."
  fi
  HTTP_RETURN_CODE=$(echo echo $1 | tail -1 | sed -e 's/^.*\([0-9][0-9][0-9]\)$/\1/')
}

echo "# 1. Create Collection Tests"
echo "=== Testing Collection Creation ==="

echo "# Test 1.1: Create collection with name only"
make_request "POST" "/collections" \
    "-d '{\"name\":\"Test Collection\"}'"

echo "# Test 1.2: Create collection with name and description"
make_request "POST" "/collections" \
    "-d '{\"name\":\"Test Collection\",\"description\":\"A test collection\"}'"

echo "# Test 1.3: Create collection with empty name (should fail)"
make_request "POST" "/collections" \
    "-d '{\"name\":\"\"}'"

echo "# Test 1.4: Create collection with very long name (edge case)"
make_request "POST" "/collections" \
    "-d '{\"name\":\"$(printf 'a%.0s' {1..1000})\"}'"

echo "# 2. List Collections Tests"
echo "=== Testing Collection Listing ==="

echo "# Test 2.1: List all collections"
make_request "GET" "/collections" ""
ALL_COLLECTION_IDS=("${COLLECTION_IDS[@]}")

echo "# Test 2.2: List collections with pagination"
make_request "GET" "/collections?offset=0&limit=10" ""

echo "# Test 2.3: List collections with invalid pagination (edge cases)"
make_request "GET" "/collections?offset=-1&limit=10" ""
make_request "GET" "/collections?offset=0&limit=0" ""
make_request "GET" "/collections?offset=0&limit=1001" ""

echo "# Test 2.4: List specific collections by IDs"
# Join array elements with commas
COMMA_SEPARATED_IDS=$(echo "${ALL_COLLECTION_IDS[*]}" | sed -e "s/ /,/")
make_request "GET" "/collections?ids=${COMMA_SEPARATED_IDS}" ""
echo "# 3. Get Collection Tests"
echo "=== Testing Collection Retrieval ==="

echo "# Test 3.1: Get collection by ID"
make_request "GET" "/collections/${ALL_COLLECTION_IDS[0]}" ""

echo "# Test 3.2: Get non-existent collection"
make_request "GET" "/collections/00000000-0000-0000-0000-000000000000" ""

echo "# Test 3.3: Get collection with invalid UUID format"
make_request "GET" "/collections/invalid-uuid" ""

echo "# 4. Update Collection Tests"
echo "=== Testing Collection Updates ==="

echo "# Test 4.1: Update collection name"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}" \
    "-d '{\"name\":\"Updated Collection Name\"}'"

echo "# Test 4.2: Update collection description"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}" \
    "-d '{\"description\":\"Updated description\"}'"

echo "# Test 4.3: Update both name and description"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}" \
    "-d '{\"name\":\"Updated Name\",\"description\":\"Updated description\"}'"

echo "# Test 4.4: Generate description"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}" \
    "-d '{\"generate_description\":true}'"

echo "# Test 4.5: Invalid update (both description and generate_description)"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}" \
    "-d '{\"description\":\"New description\",\"generate_description\":true}'"

echo "# 5. Delete Collection Tests"
echo "=== Testing Collection Deletion ==="

echo "# Test 5.1: Delete existing collection"
make_request "DELETE" "/collections/${ALL_COLLECTION_IDS[0]}" ""

echo "# Test 5.2: Delete non-existent collection"
make_request "DELETE" "/collections/00000000-0000-0000-0000-000000000000" ""

echo "# 6. Document Management Tests"
echo "=== Testing Document Management ==="

echo "# Test 6.1: Add document to collection"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}/documents/456e789a-b12c-34d5-e678-901234567890" ""

echo "# Test 6.2: List documents in collection"
make_request "GET" "/collections/${ALL_COLLECTION_IDS[0]}/documents?offset=0&limit=10" ""

echo "# Test 6.3: List documents with invalid pagination"
make_request "GET" "/collections/${ALL_COLLECTION_IDS[0]}/documents?offset=-1&limit=10" ""
make_request "GET" "/collections/${ALL_COLLECTION_IDS[0]}/documents?offset=0&limit=1001" ""

echo "# Test 6.4: Remove document from collection"
make_request "DELETE" "/collections/${ALL_COLLECTION_IDS[0]}/documents/456e789a-b12c-34d5-e678-901234567890" ""

echo "# 7. User Management Tests"
echo "=== Testing User Management ==="

echo "# Test 7.1: List users in collection"
make_request "GET" "/collections/${ALL_COLLECTION_IDS[0]}/users?offset=0&limit=10" ""

echo "# Test 7.2: List users with invalid pagination"
make_request "GET" "/collections/${ALL_COLLECTION_IDS[0]}/users?offset=-1&limit=10" ""
make_request "GET" "/collections/${ALL_COLLECTION_IDS[0]}/users?offset=0&limit=1001" ""

echo "# Test 7.3: Add user to collection"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}/users/789a012b-c34d-5e6f-a789-012345678901" ""

echo "# Test 7.4: Remove user from collection"
make_request "DELETE" "/collections/${ALL_COLLECTION_IDS[0]}/users/789a012b-c34d-5e6f-a789-012345678901" ""

echo "# 8. Entity Extraction Tests"
echo "=== Testing Entity Extraction ==="

echo "# Test 8.1: Extract entities with default settings"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}/extract" \
    "-d '{\"run_type\":\"RUN\"}'"

echo "# Test 8.2: Extract entities with custom settings"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}/extract" \
    "-d '{\"run_type\":\"RUN\",\"settings\":{\"model\":\"gpt-4\"}}'"

echo "# Test 8.3: Extract entities without orchestration"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}/extract" \
    "-d '{\"run_type\":\"RUN\",\"run_with_orchestration\":false}'"

echo "# Test 8.4: Get estimate only"
make_request "POST" "/collections/${ALL_COLLECTION_IDS[0]}/extract" \
"-d '{\"run_type\":\"ESTIMATE\"}'"
