#!/usr/bin/env bash
#
# repin-existing-to-v2_6.sh — one-shot: pin EXISTING 7DTD instances to the
# frozen v2.6 Steam branch so the controller's version-lock gate protects them.
#
# WHY: instances created before the V3.0-readiness work have no provider_id in
# config_values. The lock-gate then treats them as "not explicitly pinned"
# (empty -> safe default of v2_6 on a plain Update, but NOT gated against a
# deliberate switch). Writing provider_id=v2_6 makes them fully locked: an
# Update re-validates 2.6, and switching versions needs a typed confirm.
#
# SAFETY: metadata-only. This writes a JSON key on the DB row. It does NOT
# restart containers, does NOT run SteamCMD, does NOT touch the running game.
# Only rows that do NOT already have a provider_id are changed (idempotent).
#
# ORDERING (important): run this AFTER the new agent + controller are deployed.
# The agent's module.yaml must contain the `v2_6` provider, otherwise an Update
# that resolves provider_id=v2_6 finds no match and falls back to
# UpdateProviders[0]. (Once the new module.yaml is live, UpdateProviders[0] is
# itself v2_6, so even the fallback is safe — but deploy first to be correct.)
#
# USAGE:
#   DATABASE_URL=postgres://user:pass@host:5432/panel ./repin-existing-to-v2_6.sh           # dry-run (shows what WOULD change)
#   DATABASE_URL=postgres://user:pass@host:5432/panel ./repin-existing-to-v2_6.sh --apply    # actually write
#
# DATABASE_URL must be the same connection string the controller uses (on the
# controller host — kaiten — read it from the controller's env/systemd unit).
# Requires `psql` on PATH.
set -euo pipefail

DB="${DATABASE_URL:-}"
if [[ -z "$DB" ]]; then
  echo "ERROR: set DATABASE_URL (same string the controller uses)." >&2
  exit 1
fi

APPLY=0
[[ "${1:-}" == "--apply" ]] && APPLY=1

echo "== Current 7DTD instances and their pinned version =="
psql "$DB" -P pager=off -c "
  SELECT instance_id,
         COALESCE(config_values->>'provider_id', '(none)') AS provider_id,
         status
  FROM instances WHERE module_id = '7dtd' ORDER BY instance_id;"

echo
echo "== Rows that WOULD be re-pinned to v2_6 (no provider_id yet) =="
psql "$DB" -P pager=off -c "
  SELECT instance_id
  FROM instances
  WHERE module_id = '7dtd' AND NOT (config_values ? 'provider_id')
  ORDER BY instance_id;"

if [[ "$APPLY" -ne 1 ]]; then
  echo
  echo "DRY-RUN. Re-run with --apply to write provider_id=v2_6 to the rows above."
  exit 0
fi

echo
echo "== Applying: setting provider_id=v2_6 on un-pinned 7DTD rows =="
psql "$DB" -P pager=off -c "
  UPDATE instances
  SET config_values = jsonb_set(COALESCE(config_values, '{}'::jsonb), '{provider_id}', '\"v2_6\"', true),
      updated_at = NOW()
  WHERE module_id = '7dtd' AND NOT (config_values ? 'provider_id');"

echo
echo "== Result =="
psql "$DB" -P pager=off -c "
  SELECT instance_id, config_values->>'provider_id' AS provider_id
  FROM instances WHERE module_id = '7dtd' ORDER BY instance_id;"

echo
echo "Done. Existing 7DTD servers are now pinned to v2.6 (frozen)."
echo "No containers were restarted; nothing was downloaded."
