#!/usr/bin/env bash
# Idempotent seed for /docker/whp/settings.json sitesmith block.
set -euo pipefail

SETTINGS=/docker/whp/settings.json
if [ ! -f "$SETTINGS" ]; then
    echo "settings.json not present — skipping seed"
    exit 0
fi

command -v jq >/dev/null 2>&1 || { echo "FATAL: jq required" >&2; exit 1; }

read -r -d '' DEFAULTS <<'EOF' || true
{
  "default_monthly_cap": 50,
  "max_output_tokens": 8000,
  "max_input_tokens": 12000,
  "max_history_turns": 6,
  "max_cost_per_user_per_day_usd": 2.00,
  "max_cost_per_server_per_day_usd": 50.00,
  "per_user_rate_limit_per_minute": 6,
  "litellm_tier": "advanced",
  "alert_at_pct_of_server_cap": 80,
  "prompt_injection_guard": false,
  "model_pricing": {
    "claude-sonnet-4-6": {
      "input_per_mtok": 3.00,
      "output_per_mtok": 15.00,
      "cached_per_mtok": 0.30
    }
  }
}
EOF

NEW=$(jq --argjson d "$DEFAULTS" 'if has("sitesmith") then . else .sitesmith = $d end' "$SETTINGS")

TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT

printf '%s\n' "$NEW" > "$TMP"
chmod --reference="$SETTINGS" "$TMP" 2>/dev/null || chmod 0640 "$TMP"
chown --reference="$SETTINGS" "$TMP" 2>/dev/null || true
mv "$TMP" "$SETTINGS"

echo "sitesmith settings seeded (or already present)"
