#!/usr/bin/env bash
# Build: pre-arrival sequence
# A time-based automation that fires 36 hours before arrival. This is the one that makes the silent-arrivals script stop finding anything, which is the point.
#
# Category : Operations
# Safety   : WRITES DATA, read before running
# Uses     : workflows create, email-templates list
# Returns  : A workflow on reservation.arrival_date with a negative offset.
#
# Requires the SendSquared CLI and jq:
#   curl -fsSL https://cli.sendsquared.com/install.sh | bash
#   sendsquared auth login
#
# Free to use and adapt. https://sendsquared.com/cli/scripts/#build-pre-arrival

# Build the pre-arrival sequence. Run once.
# Usage: ./build-pre-arrival.sh <emailTemplateId> [hoursBefore]
set -euo pipefail
EMAIL=${1:?pass an email template id, see: sendsquared email-templates list}
HOURS=${2:-36}

STEPS=$(cat <<JSON
[
  { "type": "Email", "value": $EMAIL, "label": "Pre-arrival details and directions" }
]
JSON
)

# time_based triggers need a sync interval: how often to re-evaluate who is due.
sendsquared workflows create \
  --name "Pre-arrival (${HOURS}h before)" \
  --description "Sends arrival details ${HOURS} hours before check-in." \
  --trigger-type time_based \
  --trigger-value reservation.arrival_date \
  --time-offset "-${HOURS}h" \
  --sync-interval 6h \
  --steps "$STEPS"
