#!/bin/sh
set -eu

WORKFLOW_TEMPLATE="${N8N_WORKFLOW_TEMPLATE:-/opt/mc-cars/workflows/01-qualification-payment-email.json}"
WORKFLOW_RENDERED="/tmp/01-qualification-payment-email.rendered.json"
WORKFLOW03_TEMPLATE="/opt/mc-cars/workflows/03-manual-email-send.json"
WORKFLOW03_RENDERED="/tmp/03-manual-email-send.rendered.json"
CREDENTIALS_FILE="/tmp/mc-cars-credentials.json"

required_var() {
  var_name="$1"
  eval "var_value=\${$var_name:-}"
  if [ -z "$var_value" ]; then
    echo "[n8n-bootstrap] Missing required env var: $var_name" >&2
    exit 1
  fi
}

escape_sed() {
  printf '%s' "$1" | sed -e 's/[\/&]/\\&/g'
}

required_var N8N_POSTGRES_CREDENTIAL_ID
required_var N8N_POSTGRES_CREDENTIAL_NAME
required_var N8N_SMTP_CREDENTIAL_ID
required_var N8N_SMTP_CREDENTIAL_NAME
required_var N8N_SMTP_HOST
required_var N8N_SMTP_USER
required_var N8N_SMTP_PASS
required_var N8N_PAYPAL_KAUTION_LINK
required_var N8N_PAYPAL_MIETE_LINK
required_var DB_POSTGRESDB_PASSWORD
required_var N8N_PAYMENT_WORKFLOW_ID

cat > "$CREDENTIALS_FILE" <<EOF
[
  {
    "id": "${N8N_POSTGRES_CREDENTIAL_ID}",
    "name": "${N8N_POSTGRES_CREDENTIAL_NAME}",
    "type": "postgres",
    "data": {
      "host": "db",
      "password": "${DB_POSTGRESDB_PASSWORD}"
    }
  },
  {
    "id": "${N8N_SMTP_CREDENTIAL_ID}",
    "name": "${N8N_SMTP_CREDENTIAL_NAME}",
    "type": "smtp",
    "data": {
      "host": "${N8N_SMTP_HOST}",
      "user": "${N8N_SMTP_USER}",
      "password": "${N8N_SMTP_PASS}"
    }
  }
]
EOF

if [ ! -f "$WORKFLOW_TEMPLATE" ]; then
  echo "[n8n-bootstrap] Workflow template not found: $WORKFLOW_TEMPLATE" >&2
  exit 1
fi

POSTGRES_ID_ESCAPED="$(escape_sed "$N8N_POSTGRES_CREDENTIAL_ID")"
SMTP_ID_ESCAPED="$(escape_sed "$N8N_SMTP_CREDENTIAL_ID")"
KAUTION_LINK_ESCAPED="$(escape_sed "$N8N_PAYPAL_KAUTION_LINK")"
MIETE_LINK_ESCAPED="$(escape_sed "$N8N_PAYPAL_MIETE_LINK")"

sed \
  -e "s/__POSTGRES_CREDENTIAL_ID__/${POSTGRES_ID_ESCAPED}/g" \
  -e "s/__SMTP_CREDENTIAL_ID__/${SMTP_ID_ESCAPED}/g" \
  -e "s|__PAYPAL_KAUTION_LINK__|${KAUTION_LINK_ESCAPED}|g" \
  -e "s|__PAYPAL_MIETE_LINK__|${MIETE_LINK_ESCAPED}|g" \
  "$WORKFLOW_TEMPLATE" > "$WORKFLOW_RENDERED"

echo "[n8n-bootstrap] Importing credentials"
n8n import:credentials --input="$CREDENTIALS_FILE"

echo "[n8n-bootstrap] Importing workflow 01"
n8n import:workflow --input="$WORKFLOW_RENDERED"

# Process and import workflow 03 - Manual Email Send
if [ -f "$WORKFLOW03_TEMPLATE" ]; then
  sed \
    -e "s/__POSTGRES_CREDENTIAL_ID__/${POSTGRES_ID_ESCAPED}/g" \
    -e "s/__SMTP_CREDENTIAL_ID__/${SMTP_ID_ESCAPED}/g" \
    -e "s|__PAYPAL_KAUTION_LINK__|${KAUTION_LINK_ESCAPED}|g" \
    -e "s|__PAYPAL_MIETE_LINK__|${MIETE_LINK_ESCAPED}|g" \
    "$WORKFLOW03_TEMPLATE" > "$WORKFLOW03_RENDERED"

  echo "[n8n-bootstrap] Importing workflow 03 (Manual Email Send)"
  n8n import:workflow --input="$WORKFLOW03_RENDERED"
fi

# Publish all imported workflows so they appear in the UI
echo "[n8n-bootstrap] Publishing all workflows"
WF_IDS=$(n8n list:workflow 2>/dev/null | cut -d'|' -f1 || true)
for wfid in $WF_IDS; do
  echo "[n8n-bootstrap] Publishing workflow $wfid"
  n8n publish:workflow --id="$wfid" 2>/dev/null || true
done

echo "[n8n-bootstrap] Bootstrap complete"
