#!/bin/bash
set -e

if [ -z "$SONGWHIP_API_URL" ] || [ -z "$SONGWHIP_WEBHOOK_KEY" ]; then
  echo "Error: SONGWHIP_API_URL and SONGWHIP_WEBHOOK_KEY must be set."
  exit 1
fi

echo "Creating test prereleases..."

response=$(curl -s -w "\n%{http_code}" -X POST "$SONGWHIP_API_URL/v3/prereleases/create-test-pages" \
  -H "songwhip-webhook-key: $SONGWHIP_WEBHOOK_KEY" \
  -H "Content-Type: application/json")

http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

if [ "$http_code" -eq 200 ]; then
  echo "Request executed successfully."
else
  echo "Error: Request failed with status $http_code"
  echo "Response body:"
  echo "$body"
  exit 1
fi
