#!/usr/bin/env bash

# Spec Kit 分支管理便捷工具
# 提供简化的分支管理命令

set -e

# 获取脚本目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MANAGE_SCRIPT="$SCRIPT_DIR/manage-branches.sh"

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

show_help() {
    echo "Spec Kit 分支管理工具"
    echo ""
    echo "用法: spec-branch <命令> [选项]"
    echo ""
    echo "命令:"
    echo "  status       查看分支状态"
    echo "  clean        交互式清理分支"
    echo "  merge        合并当前分支到main"
    echo "  check        检查是否适合创建新功能"
    echo ""
    echo "示例:"
    echo "  spec-branch status       # 查看当前分支状态"
    echo "  spec-branch clean        # 交互式清理多余分支"
    echo "  spec-branch merge        # 完成当前功能并合并"
    echo "  spec-branch check        # 检查分支状态"
}

main() {
    if [ ! -x "$MANAGE_SCRIPT" ]; then
        echo "错误: 找不到 manage-branches.sh 脚本" >&2
        exit 1
    fi

    case "${1:-}" in
        status|s)
            echo -e "${BLUE}📊 分支状态概览${NC}"
            "$MANAGE_SCRIPT" --status
            ;;
        clean|c)
            echo -e "${YELLOW}🧹 开始分支清理${NC}"
            "$MANAGE_SCRIPT" --cleanup
            ;;
        merge|m)
            echo -e "${GREEN}🔄 合并当前分支${NC}"
            "$MANAGE_SCRIPT" --merge
            ;;
        check)
            echo -e "${BLUE}🔍 检查分支状态${NC}"
            "$MANAGE_SCRIPT" --check
            ;;
        help|h|--help|-h)
            show_help
            ;;
        "")
            # 默认显示状态
            "$MANAGE_SCRIPT" --status
            ;;
        *)
            echo "未知命令: $1" >&2
            echo "运行 'spec-branch help' 查看帮助" >&2
            exit 1
            ;;
    esac
}

main "$@"