#!/usr/bin/env bash
# Full OMNL production activation: deploy stack, nginx domains, LXCs, optional opening journal.
set -euo pipefail

REPO_DIR="${OMNL_BANK_ROOT:-$HOME/smom-dbis-138}"
ENV_FILE="${OMNL_BANK_ENV:-$REPO_DIR/.env}"
SEED_JOURNAL="${SEED_OFFICE24_JOURNAL:-1}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/omnl-pve-ssh.sh
source "${SCRIPT_DIR}/lib/omnl-pve-ssh.sh"

log() { echo "[$(date -Iseconds)] $*"; }

cd "$REPO_DIR"
git pull --ff-only origin main

log "=== 1/5 Deploy OMNL bank stack on dev ==="
bash scripts/deployment/deploy-omnl-bank-production.sh

log "=== 2/5 Install nginx domain routing (online + central bank + trade) ==="
bash scripts/deployment/deploy-omnl-domains.sh

log "=== 3/5 Sync banking portal LXCs (5825-5828) ==="
bash scripts/deployment/sync-ali-omnl-banking-portal-lxcs.sh --apply

log "=== 4/5 Push Fineract/API env to each LXC ==="
if [[ -f "$ENV_FILE" ]]; then
  for CTID in 5825 5826 5827 5828; do
    bash scripts/deployment/push-portal-env-to-lxc.sh "$CTID" || log "WARN: env push failed for CT $CTID"
    omnl_pve_ssh "pct exec ${CTID} -- bash ${OMNL_PORTAL_ROOT:-/srv/zardasht-portal}/scripts/deployment/start-omnl-banking-portal.sh" 2>/dev/null || true
  done
else
  log "WARN: no $ENV_FILE — skip LXC Fineract env"
fi

log "=== 5/5 Office 24 opening journal (activate live ledgers) ==="
MATERIAL_OPENING_USD="${OMNL_MATERIAL_OPENING_THRESHOLD:-10000000}"
STATUS_FILE="$REPO_DIR/config/omnl-hospitallers-office24-status.v1.json"
if [[ "$SEED_JOURNAL" == "1" ]] && [[ -f "$ENV_FILE" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$ENV_FILE"
  set +a
  if [[ -f "$STATUS_FILE" ]] && grep -q '"fundingPosted"[[:space:]]*:[[:space:]]*true' "$STATUS_FILE" 2>/dev/null; then
    log "Office 24 fundingPosted=true in status SSOT — skip single-leg seed (journal already live)"
  elif [[ -n "${OFFICE24_OPENING_M1_USD:-}" ]]; then
    opening_amt="${OFFICE24_OPENING_M1_USD//,/}"
    if awk -v amt="$opening_amt" -v lim="$MATERIAL_OPENING_USD" 'BEGIN { exit !(amt >= lim) }'; then
      log "Material opening amount ($OFFICE24_OPENING_M1_USD) — skip single-leg seed (use 1b canary/execute matrix)"
    else
      node scripts/deployment/seed-office-24-opening-journal.mjs
      bash scripts/deployment/deploy-omnl-bank-production.sh
    fi
  else
    log "OFFICE24_OPENING_M1_USD not set — ledger reads live Fineract only (no seed)"
  fi
else
  log "Journal seed skipped (SEED_OFFICE24_JOURNAL=$SEED_JOURNAL)"
fi

log "=== Production URLs ==="
log "  Central Bank:   https://secure.d-bis.org/central-bank"
log "  Online banking: https://online.omdnl.org/central-bank"
log "  Office 24:      https://office24.omdnl.org/office-24"
log "  DBIS Trade:     https://exchange.d-bis.org/trade"
log "  Digital Swap:   https://digital.omdnl.org/swap"
log "=== Health ==="
curl -sf http://127.0.0.1:3011/api/v1/settlement/health | head -c 120 || true
echo
curl -sf http://127.0.0.1:3011/api/v1/settlement/money-supply/public | head -c 300 || true
echo
log "Activation complete"
