#!/usr/bin/env bash
# test_flow.sh — end-to-end smoke test
BASE_URL="https://be1net.ch"
DB_HOST="localhost"
DB_NAME="belugadev_beluga_association"
DB_USER="belugadev_ba_dbuser"
DB_PASS="ixa2Ig8H*E&M"
TS=$(date +'%Y-%m-%d %H:%M:%S')
PAYLOAD=$(cat <<EOF
{
  "member_id": "TEST_FLOW_001",
  "items": [{"inventory_item":"WidgetA","quantity":7,"denomination":"Each"}],
  "acknowledged": "Y",
  "submitted_at": "$TS"
}
EOF
)
echo "→ Submitting to inventory_tally.php…"
echo "$PAYLOAD" | curl -s -X POST -H "Content-Type: application/json" -d @- "$BASE_URL/includes/inventory_tally.php" | tee /dev/stdout
echo "→ Verifying MySQL insert…"
mysql -h"$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "SELECT id, member_id, inventory_item, quantity, denomination, submitted_at FROM tally_submissions WHERE member_id = 'TEST_FLOW_001' ORDER BY submitted_at DESC LIMIT 1;"
echo "→ Verifying submission log…"
mysql -h"$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "SELECT id, event, status, created_at FROM submission_logs WHERE member_id = 'TEST_FLOW_001' ORDER BY created_at DESC LIMIT 1;"
echo "→ Testing CRM updater…"
CRM_PAYLOAD=$(cat <<EOF
{"member_id":"TEST_FLOW_001","tally_tnc":"Y","tally_date":"$TS"}
EOF
)
echo "$CRM_PAYLOAD" | curl -s -X POST -H "Content-Type: application/json" -d @- "$BASE_URL/includes/bitrix24_update_contact.php" | tee /dev/stdout
echo "✅ All tests complete."
BASE_URL="https://be1net.ch/BAA-AMS"
# (and any DB_HOST/DB_USER/DB_PASS if the script uses them)
