#!/usr/bin/env bash
# move_source_wids_directory_after_merge
# 請在合併後, 更新 hosts 前處理

set -uo pipefail

if [ $# -lt 4 ]; then
  echo "用法: $0 <environment:test/live> <dest_directory> <target_world_id> <source_world_id1> [source_world_id2 ...]" >&2
  exit 1
fi

environment="$1"; shift
dest_directory="$1"; shift
target_world_id="$1"; shift # target 不處理（依需求保留但不使用）

# source world ids（第 4 個參數起）
source_world_ids=("$@")

SSH_OPT=(
  -o StrictHostKeyChecking=no
  -o BatchMode=yes
  -o ConnectTimeout=10
  -o ServerAliveInterval=10
  -o ServerAliveCountMax=3
)

log_ts() { date '+%Y-%m-%d %H:%M:%S'; }
log_info() { echo "[${0##*/}] $(log_ts) INFO: $*"; }
log_moved() { echo "[${0##*/}] $(log_ts) MOVED: $*"; }
log_error() { echo "[${0##*/}] $(log_ts) ERROR: $*" >&2; }

fail_count=0

for world_id in "${source_world_ids[@]}"; do
  if ! [[ "${world_id}" =~ ^[0-9]+$ ]]; then
    log_error "world_id 必須是數字：${world_id}"
    fail_count=$((fail_count + 1))
    continue
  fi

  SET_ID=$((world_id / 100))

  if [ "${environment}" == "live" ]; then
    MACHINE="WS${world_id}"
  elif [ "${environment}" == "test" ]; then
    if [ "${SET_ID}" -ge 10 ] && [ "${SET_ID}" -lt 20 ]; then
      MACHINE="MERGE_ASIA"
    elif [ "${SET_ID}" -ge 20 ] && [ "${SET_ID}" -lt 30 ]; then
      MACHINE="MERGE_US"
    elif [ "${SET_ID}" -ge 30 ] && [ "${SET_ID}" -lt 40 ]; then
      MACHINE="MERGE_EU"
    else
      log_error "Unknown SET_ID ${SET_ID} (world_id=${world_id})"
      fail_count=$((fail_count + 1))
      continue
    fi
  else
    log_error "Unknown environment ${environment}, must be test or live"
    exit 1
  fi

  dest_path="\$HOME/prepare/${dest_directory}/servers${SET_ID}"
  log_info "START world_id=${world_id} SET_ID=${SET_ID} MACHINE=${MACHINE} DEST=${dest_path}"

  if ! ssh "${SSH_OPT[@]}" "${MACHINE}" "mkdir -p \"${dest_path}\""; then
    log_error "mkdir failed on ${MACHINE} (world_id=${world_id}, SET_ID=${SET_ID})"
    fail_count=$((fail_count + 1))
    continue
  fi

  # 核心修正：
  # - 避免 *${world_id}* 的子字串誤中
  # - 保留「不落在更長數字中間」的修正：world_id 左側必須是字串開頭或非數字
  # - 右側允許為數字（例如 ...11061 / ...11111），避免漏搬
  remote_dir=$(
    ssh "${SSH_OPT[@]}" "${MACHINE}" \
      "bash -lc 'shopt -s nullglob
                cd \"\$HOME/servers${SET_ID}\" || exit 0
                for d in *; do
                  [[ -d \"\$d\" ]] || continue
                  if [[ \"\$d\" =~ (^|[^0-9])${world_id} ]]; then
                    printf \"%s\n\" \"\$d\"
                  fi
                done
                exit 0'"
  ) || {
    log_error "list dirs failed on ${MACHINE} (world_id=${world_id}, SET_ID=${SET_ID})"
    fail_count=$((fail_count + 1))
    continue
  }

  if [ -z "${remote_dir}" ]; then
    log_info "NO_MATCH world_id=${world_id} on ${MACHINE} at \$HOME/servers${SET_ID}"
    continue
  fi

  moved_this_wid=0

  while IFS= read -r dir; do
    [ -n "${dir}" ] || continue

    if ssh "${SSH_OPT[@]}" "${MACHINE}" \
      "cd \"\$HOME/servers${SET_ID}\" && mv \"${dir}\" \"${dest_path}/.\""; then
      moved_this_wid=$((moved_this_wid + 1))
      log_moved "machine=${MACHINE} world_id=${world_id} set_id=${SET_ID} dir=${dir} -> ${dest_path}/"
    else
      log_error "mv failed on ${MACHINE} (world_id=${world_id}, set_id=${SET_ID}, dir=${dir})"
      fail_count=$((fail_count + 1))
      continue
    fi
  done <<< "${remote_dir}"

  log_info "END world_id=${world_id} moved=${moved_this_wid} MACHINE=${MACHINE}"
done

if [ "${fail_count}" -gt 0 ]; then
  log_error "DONE with errors: fail_count=${fail_count}"
  exit 2
fi

log_info "DONE OK"
exit 0

