#!/usr/bin/env bash
set -euo pipefail

PDF_PATH="${1:-tests/SWCPI25011315283.pdf}"
ENVIRONMENT="${2:-test}"

if [ ! -f "$PDF_PATH" ]; then
  echo "Error: PDF file not found: $PDF_PATH"
  exit 1
fi

FUNCTION_NAME="glimpse2-${ENVIRONMENT}"
PAYLOAD_FILE=$(mktemp)
python3 -c "import base64, json, sys; json.dump({'pdf_base64': base64.b64encode(open(sys.argv[1], 'rb').read()).decode()}, open(sys.argv[2], 'w'))" "$PDF_PATH" "$PAYLOAD_FILE"

echo "Invoking ${FUNCTION_NAME} with ${PDF_PATH}..."
aws lambda invoke \
  --function-name "$FUNCTION_NAME" \
  --payload file://"$PAYLOAD_FILE" \
  --cli-binary-format raw-in-base64-out \
  /tmp/lambda_response.json

rm -f "$PAYLOAD_FILE"

echo "Response:"
python3 -m json.tool /tmp/lambda_response.json 2>/dev/null || cat /tmp/lambda_response.json
