#!/bin/bash
########################################
# merge_config.sh — 合併腳本共用配置
#
# 所有差異邏輯集中在此，透過 MERGE_MODE 切換。
# 呼叫端需先設定 MERGE_MODE=single|multiple
#
# Usage:
#   export MERGE_MODE=single   # 或 multiple
#   source "$(dirname "$0")/merge_config.sh"
########################################

# 取得腳本所在目錄（供其他腳本引用同目錄下的工具）
MERGE_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 預設 mode
: "${MERGE_MODE:=single}"

########################################
# Area dispatch
########################################

# 依 SET_ID 取得 area 名稱
get_area_by_setid() {
    local mode="${1:-$MERGE_MODE}" sid="$2"
    case "$mode" in
        single)
            echo "tw"
            ;;
        multiple)
            if (( sid >= 10 && sid < 20 )); then echo "asia"
            elif (( sid >= 20 && sid < 30 )); then echo "us"
            elif (( sid >= 30 && sid < 40 )); then echo "eu"
            else
                echo "Error: unsupported SET_ID '$sid' (only 10~39)" >&2
                return 1
            fi
            ;;
        *)
            echo "Error: unknown MERGE_MODE '$mode'" >&2
            return 1
            ;;
    esac
}

########################################
# Machine dispatch (test 環境)
########################################

# 依 SET_ID 取得 test 環境的機器名
get_test_machine_by_setid() {
    local mode="${1:-$MERGE_MODE}" sid="$2"
    case "$mode" in
        single)
            echo "MERGE"
            ;;
        multiple)
            local area
            area=$(get_area_by_setid "$mode" "$sid") || return 1
            case "$area" in
                asia) echo "MERGE_ASIA" ;;
                us)   echo "MERGE_US" ;;
                eu)   echo "MERGE_EU" ;;
            esac
            ;;
    esac
}

# 取得 test 環境的 common 機器（AccountDB 所在）
get_test_common_machine() {
    local mode="${1:-$MERGE_MODE}"
    case "$mode" in
        single)   echo "MERGE" ;;
        multiple) echo "MERGE_COMMON" ;;
    esac
}

# 取得 test 環境的所有機器（含 common）
get_all_test_machines() {
    local mode="${1:-$MERGE_MODE}"
    case "$mode" in
        single)   echo "MERGE" ;;
        multiple) echo "MERGE_COMMON MERGE_ASIA MERGE_US MERGE_EU" ;;
    esac
}

# 取得 test 環境的 area 機器（不含 common）
get_area_test_machines() {
    local mode="${1:-$MERGE_MODE}"
    case "$mode" in
        single)   echo "MERGE" ;;
        multiple) echo "MERGE_ASIA MERGE_US MERGE_EU" ;;
    esac
}

########################################
# Machine dispatch (依環境自動切換)
########################################

# 依 env + SET_ID 取得 MS 機器名
get_machine_by_env_and_setid() {
    local mode="${1:-$MERGE_MODE}" env="$2" sid="$3"
    if [[ "$env" == "live" ]]; then
        echo "MS${sid}"
    else
        get_test_machine_by_setid "$mode" "$sid"
    fi
}

# 依 env + world_id 取得 WS 機器名
get_ws_machine_by_env() {
    local mode="${1:-$MERGE_MODE}" env="$2" wid="$3"
    if [[ "$env" == "live" ]]; then
        echo "WS${wid}"
    else
        local sid=$((wid / 100))
        get_test_machine_by_setid "$mode" "$sid"
    fi
}

# 依 env + mode 取得 AccountDB 的 SSH host
get_accountdb_host_by_env() {
    local mode="${1:-$MERGE_MODE}" env="$2"
    if [[ "$env" == "live" ]]; then
        echo "ACCOUNTDB"
    else
        get_test_common_machine "$mode"
    fi
}

########################################
# Generator script dispatch
########################################

get_generator_script() {
    local mode="${1:-$MERGE_MODE}" env="$2"
    if [[ "$env" == "live" ]]; then
        echo "generate_merge_preparation"
    else
        echo "generate_merge_preparation_test"
    fi
}

########################################
# removebackup machine list (for test env)
########################################

get_removebackup_machines() {
    local mode="${1:-$MERGE_MODE}"
    local -n _result_arr=$2  # nameref to caller's array
    local set_filters_str="$3"  # space-separated SET_IDs or empty

    _result_arr=()

    if [[ "$mode" == "single" ]]; then
        _result_arr+=("MERGE")
    else
        _result_arr+=("MERGE_COMMON")
        if [[ -z "$set_filters_str" ]]; then
            _result_arr+=("MERGE_ASIA" "MERGE_US" "MERGE_EU")
        else
            local need_asia=0 need_us=0 need_eu=0
            for sid in $set_filters_str; do
                (( sid >= 10 && sid < 20 )) && need_asia=1
                (( sid >= 20 && sid < 30 )) && need_us=1
                (( sid >= 30 && sid < 40 )) && need_eu=1
            done
            (( need_asia )) && _result_arr+=("MERGE_ASIA")
            (( need_us ))   && _result_arr+=("MERGE_US")
            (( need_eu ))   && _result_arr+=("MERGE_EU")
        fi
    fi
}
