#!/bin/bash
# Post-deploy form smoke test.
# Sends a COMPLETE valid TEST lead to the live /submit endpoint with the
# X-Health-Check header, so the worker returns the Zapier + D1 status in its
# response (and skips the failure-alert email).
#
# Usage:  bash test-form.sh <domain>
#   e.g.  bash test-form.sh go.yourdomain.com
#
# Exits 0 only when the lead was fully delivered (HTTP 200, success:true,
# CRM webhook accepted it, AND the required D1 backup row was written).
# Anything else prints what failed and exits 1.
#
# WARNING: this creates a REAL lead. It writes a row to the live D1 `submissions`
# table AND fires the client's Zapier webhook (X-Health-Check only suppresses the
# alert email, not the writes). The lead is flagged first_name=TEST - clean it up
# in the CRM / delete the D1 row afterward.

set -euo pipefail
DOMAIN="${1:?Usage: bash test-form.sh <domain>   (e.g. go.yourdomain.com)}"

echo "Sending complete TEST lead to https://$DOMAIN/submit ..."
RAW="$(curl -sS -X POST "https://$DOMAIN/submit" \
  -H "Content-Type: application/json" \
  -H "X-Health-Check: 1" \
  -d '{
    "first_name": "TEST",
    "last_name": "SmokeTest",
    "email": "test@example.com",
    "phone": "5555550100",
    "address": "123 Test Street, Charlotte, NC 28217",
    "property_type": "Single Family",
    "owns_land": "Yes",
    "listed": "No",
    "condition": "Needs some work",
    "why_selling": "Post-deploy smoke test",
    "timeline": "ASAP",
    "asking_price": "100000",
    "utm_source": "deploy-test",
    "utm_medium": "smoke-test",
    "utm_campaign": "post-deploy",
    "utm_content": "test-form.sh",
    "utm_term": "",
    "page_url": "https://'"$DOMAIN"'/",
    "referrer": "test-form.sh",
    "sms_consent": "yes"
  }' \
  -w '\n%{http_code}')"

STATUS="$(printf '%s' "$RAW" | tail -n 1)"
BODY="$(printf '%s' "$RAW" | sed '$d')"
echo "Response [HTTP $STATUS]: $BODY"
echo ""

ZAPIER="$(printf '%s' "$BODY" | grep -oE '"zapier":("[^"]*"|-?[0-9]+)' | cut -d: -f2 | tr -d '"')"
D1="$(printf '%s' "$BODY" | grep -oE '"d1":"[^"]*"' | cut -d: -f2 | tr -d '"')"

FAIL=0
if [ "$STATUS" != "200" ]; then
  echo "FAIL: expected HTTP 200, got HTTP $STATUS"
  FAIL=1
fi
if ! printf '%s' "$BODY" | grep -q '"success":true'; then
  echo "FAIL: the response is not success:true"
  FAIL=1
fi
case "$ZAPIER" in
  2[0-9][0-9]) echo "OK:   CRM webhook accepted the lead (HTTP $ZAPIER)";;
  unconfigured) echo "FAIL: ZAPIER_WEBHOOK_URL is not set on the Pages project (dashboard → Settings → Variables and Secrets), or you haven't redeployed since setting it"; FAIL=1;;
  *) echo "FAIL: CRM webhook did not accept the lead (status: ${ZAPIER:-none})"; FAIL=1;;
esac
case "$D1" in
  ok) echo "OK:   backup row written to the D1 submissions table";;
  unbound|skipped) echo "FAIL: the REQUIRED D1 backup database is not bound — do SETUP #3 (create D1, bind as 'db', run the table command), redeploy, and re-test"; FAIL=1;;
  *) echo "FAIL: D1 backup write failed (status: ${D1:-none})"; FAIL=1;;
esac

echo ""
if [ "$FAIL" -ne 0 ]; then
  echo "RESULT: FAILED — fix the item(s) above and run this test again before sending traffic."
  exit 1
fi
echo "RESULT: PASS. Now clean up the TEST lead in your CRM (and the D1 row, if using D1) so it doesn't skew your data."
