#!/usr/bin/env bash
set -euo pipefail
SETTINGS=/docker/whp/settings.json
[ -f "${SETTINGS}" ] || echo '{}' > "${SETTINGS}"
command -v jq >/dev/null 2>&1 || { echo "FATAL: jq required"; exit 1; }

TMP=$(mktemp); trap 'rm -f "$TMP"' EXIT
jq '
  .boot_orchestrator //= {} |
  .boot_orchestrator.enabled                        //= true  |
  .boot_orchestrator.customer_batch_size            //= 5     |
  .boot_orchestrator.customer_batch_delay_sec       //= 10    |
  .boot_orchestrator.tier_healthcheck_timeout_sec   //= 60    |
  .boot_orchestrator.boot_total_timeout_sec         //= 600   |
  .customer_watchdog //= {} |
  .customer_watchdog.enabled                                //= true  |
  .customer_watchdog.max_restarts_per_tick                  //= 3     |
  .customer_watchdog.settle_window_sec                      //= 30    |
  .customer_watchdog.post_start_verify_sec                  //= 5     |
  .customer_watchdog.alert_after_consecutive_failures       //= 5     |
  .canonical_hosting_domain //= "anhonesthost.com"
' "${SETTINGS}" > "${TMP}"

chmod --reference="${SETTINGS}" "${TMP}" 2>/dev/null || chmod 0640 "${TMP}"
chown --reference="${SETTINGS}" "${TMP}" 2>/dev/null || true
mv "${TMP}" "${SETTINGS}"
echo "settings.json: boot_orchestrator and customer_watchdog defaults applied"
