#!/bin/bash

# Bearer token (best to set this as an environment variable)
BEARER_TOKEN="<YOUR TOKEN>"

# Google TTS API endpoint
TTS_API_URL="https://texttospeech.googleapis.com/v1/text:synthesize"

# Request payload
read -r -d '' PAYLOAD <<EOF
{
  "input": {
    "text": "Hello, this is a sample text to speech synthesis request."
  },
  "voice": {
    "languageCode": "en-US",
    "name": "en-US-Wavenet-D"
  },
  "audioConfig": {
    "audioEncoding": "MP3"
  }
}
EOF

# Make the request and extract audioContent
AUDIO_BASE64=$(curl -s -X POST "$TTS_API_URL" \
  -H "Authorization: Bearer $BEARER_TOKEN" \
  -H "Content-Type: application/json" \
  -d "$PAYLOAD" | jq -r '.audioContent')

# Decode base64 and save to MP3
echo "$AUDIO_BASE64" | base64 --decode > output.mp3

echo "✅ MP3 file saved to: output.mp3"
