#!/bin/bash

product_code_list=("nomo")

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
MAGENTA='\033[0;35m'
RESET='\033[0m'

function execute_command() {
    command="$1"
    message="$2"
    exit_on_failure=${3:-true}

    echo "${GREEN}执行命令：${command}${RESET}"

    eval "${command}"
    status=$?

    if [[ ${status} -ne 0 ]]; then
        echo "${RED}错误：${message}${RESET}"
        if [[ "$exit_on_failure" == "true" ]]; then
            exit ${status}
        else
            return ${status}
        fi
    fi
}

for product_code in "${product_code_list[@]}"; do
    echo "${MAGENTA}正在合并 ${product_code} 代码...${RESET}"
    # 切换并拉取 ${product_code}-prod 分支
    execute_command "git checkout ${product_code}-prod" "切换至 ${product_code}-prod 分支失败"
    execute_command "git pull" "拉取 ${product_code}-prod 分支最新内容失败"

    # 合并 ${product_code}-prod 到 ${product_code}-test
    execute_command "git checkout ${product_code}-test" "切换至 ${product_code}-test 分支失败"
    execute_command "git pull" "拉取 ${product_code}-test 分支最新内容失败"
    execute_command "git merge ${product_code}-prod" "合并 ${product_code}-prod 到 ${product_code}-test 分支失败" "false"
    merge_p2t_status=$?
    if [ $merge_p2t_status -ne 0 ]; then
        # 检查是否有冲突
        if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
            echo "${YELLOW}检测到合并冲突，请手动解决冲突...${RESET}"
            echo "${YELLOW}解决冲突后，按任意键继续...${RESET}"
            read -n 1 -s -r -p ""

            if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
                echo "${RED}错误：仍存在未解决的冲突，请处理后再继续${RESET}"
                exit 1
            fi

            execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
        else
            exit ${merge_p2t_status}
        fi
    fi
    execute_command "git push" "推送 ${product_code}-test 分支失败"

    # 合并 ${product_code}-test 到 master
    execute_command "git checkout master" "切换至 master 分支失败"
    execute_command "git pull" "拉取 master 分支最新内容失败"
    execute_command "git merge ${product_code}-test" "合并 ${product_code}-test 到 master 分支失败" "false"
    merge_t2m_status=$?
    if [ $merge_t2m_status -ne 0 ]; then
        # 检查是否有冲突
        if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
            echo "${YELLOW}检测到合并冲突，请手动解决冲突...${RESET}"
            echo "${YELLOW}解决冲突后，按任意键继续...${RESET}"
            read -n 1 -s -r -p ""

            if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
                echo "${RED}错误：仍存在未解决的冲突，请处理后再继续${RESET}"
                exit 1
            fi

            execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
        else
            exit ${merge_t2m_status}
        fi
    fi
    execute_command "git push" "推送至 master 分支失败"
done

# # 合并 master 到 ${product_code}-dev
# execute_command "git checkout ${product_code}-dev" "切换至 ${product_code}-dev 分支失败"
# execute_command "git pull" "拉取 ${product_code}-dev 分支最新内容失败"
# execute_command "git merge master" "合并 master 到 ${product_code}-dev 分支失败" "false"
# merge_m2d_status=$?
# if [ $merge_m2d_status -ne 0 ]; then
#     # 检查是否有冲突
#     if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
#         echo "${YELLOW}检测到合并冲突，请手动解决冲突...${RESET}"
#         echo "${YELLOW}解决冲突后，按任意键继续...${RESET}"
#         read -n 1 -s -r -p ""
#         if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
#             echo "${RED}错误：仍存在未解决的冲突，请处理后再继续${RESET}"
#             exit 1
#         fi
#         execute_command "git commit -m 'chore(merge): 解决冲突'" "提交冲突解决方案失败"
#     else
#         exit ${merge_m2d_status}
#     fi
# fi
# execute_command "git push" "推送至 ${product_code}-dev 分支失败"

# 切换到 xiaom 分支
execute_command "git checkout xiaom" "切换至 xiaom 分支失败"
