#!/usr/bin/env bash
set -e

ENDPOINT='https://queue-staging.songwhip.com/groups/wilsons-test-group-29' # Assuming the first argument to the script is the endpoint URL
N=5000 # Total number of tasks to add
BATCH_SIZE=500 # Maximum number of tasks per batch

for ((batch_start=1; batch_start<=N; batch_start+=BATCH_SIZE))
do
  # Calculate the end of the current batch
  batch_end=$((batch_start + BATCH_SIZE - 1))
  if [ $batch_end -gt $N ]; then
    batch_end=$N
  fi

  # Start of the JSON body for the current batch
  JSON_BODY='{
    "_maxRequestsPerMin": 120,
    "tasks": ['

  # Add tasks to the JSON body for the current batch
  for ((i=batch_start; i<=batch_end; i++))
  do
      JSON_BODY+='{"method": "GET", "url": "https://enjoea9a6wsm8.x.pipedream.net/'$i'"}'
    if [ $i -lt "$batch_end" ]; then
      JSON_BODY+=','
    fi
  done

  # End of the JSON body for the current batch
  JSON_BODY+=']}'

  # Use curl to call songwhip-queue on $ENDPOINT passing the JSON body of the current batch
  curl -X PUT "$ENDPOINT" -H "Content-Type: application/json" -d "$JSON_BODY"

  # Wait for 1 second before proceeding to the next batch
  sleep 1
done