#!/bin/bash

# Bug修复归档脚本
# 用途: 自动创建Bug修复归档目录结构，并生成文档模板

set -e

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

# 打印带颜色的信息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查是否在项目根目录
check_project_root() {
    if [ ! -f "FILE_ORGANIZATION.md" ]; then
        print_error "请在项目根目录运行此脚本"
        exit 1
    fi
}

# 显示使用说明
show_usage() {
    echo "使用方法: $0 <bug名称> [作者名称]"
    echo ""
    echo "示例:"
    echo "  $0 path_tracking_error \"张三\""
    echo "  $0 csv_load_issue"
    echo ""
    echo "说明:"
    echo "  - bug名称使用英文和下划线，如: path_tracking_error"
    echo "  - 作者名称可选，默认从git config获取"
    exit 1
}

# 获取当前日期
get_date() {
    date +%Y%m%d
}

# 获取作者名称
get_author() {
    if [ -n "$1" ]; then
        echo "$1"
    else
        git config user.name || echo "Unknown"
    fi
}

# 获取最新的commit信息
get_latest_commit() {
    if git rev-parse --git-dir > /dev/null 2>&1; then
        echo "$(git rev-parse --short HEAD)"
    else
        echo "N/A"
    fi
}

# 创建归档目录
create_archive_dirs() {
    local date=$1
    local bug_name=$2
    local archive_path="archives/bug_fixes/${date}_${bug_name}"

    print_info "创建归档目录: ${archive_path}"

    mkdir -p "${archive_path}/code"
    mkdir -p "${archive_path}/docs"
    mkdir -p "${archive_path}/tests"
    mkdir -p "docs/fixes"

    print_success "目录创建完成"
    echo "${archive_path}"
}

# 生成文档
create_document() {
    local date=$1
    local bug_name=$2
    local author=$3
    local commit_hash=$4
    local archive_path=$5

    local doc_name="BUG_${bug_name}_${date}.md"
    local doc_path="${archive_path}/docs/${doc_name}"
    local template_path=".claude/templates/bug_fix_template.md"

    print_info "生成文档: ${doc_name}"

    # 如果模板存在，使用模板；否则创建基本文档
    if [ -f "${template_path}" ]; then
        cp "${template_path}" "${doc_path}"

        # 替换模板变量
        sed -i "s/{{BUG_DESCRIPTION}}/${bug_name}/g" "${doc_path}"
        sed -i "s/{{DATE}}/${date}/g" "${doc_path}"
        sed -i "s/{{AUTHOR}}/${author}/g" "${doc_path}"
        sed -i "s/{{COMMIT_HASH}}/${commit_hash}/g" "${doc_path}"
        sed -i "s/{{DATE}}_{{BUG_NAME}}/${date}_${bug_name}/g" "${doc_path}"
    else
        # 创建基本文档
        cat > "${doc_path}" << EOF
# Bug修复: ${bug_name}

**日期**: ${date}
**修复者**: ${author}
**Git Commit**: ${commit_hash}

---

## Bug描述

[详细描述bug的表现]

## 复现步骤

1. [步骤1]
2. [步骤2]
3. [观察到的错误]

## 根本原因

[分析bug的根本原因]

## 修复方案

[描述修复方案]

## 修改文件清单

- \`file1.cpp\` - [修改说明]

## 测试验证

[测试结果]

## Git提交信息

\`\`\`
Commit ID: ${commit_hash}
\`\`\`

---

**归档位置**: \`archives/bug_fixes/${date}_${bug_name}/\`
EOF
    fi

    # 复制到快速查阅目录
    cp "${doc_path}" "docs/fixes/${doc_name}"

    print_success "文档生成完成"
    echo "${doc_path}"
}

# 显示下一步操作
show_next_steps() {
    local doc_path=$1
    local archive_path=$2

    echo ""
    print_success "Bug修复归档创建完成!"
    echo ""
    echo "📁 归档目录: ${archive_path}"
    echo "📄 文档位置: ${doc_path}"
    echo "📋 快速查阅: docs/fixes/"
    echo ""
    print_info "下一步操作:"
    echo "  1. 编辑文档: vim ${doc_path}"
    echo "  2. 复制修复的代码到: ${archive_path}/code/"
    echo "  3. 添加测试文件到: ${archive_path}/tests/"
    echo "  4. 提交git: git add . && git commit -m \"archive: Bug修复 - ${bug_name}\""
    echo ""
}

# 主函数
main() {
    # 检查参数
    if [ $# -lt 1 ]; then
        show_usage
    fi

    local bug_name=$1
    local author=$(get_author "$2")

    # 检查项目根目录
    check_project_root

    # 获取信息
    local date=$(get_date)
    local commit_hash=$(get_latest_commit)

    print_info "开始创建Bug修复归档"
    print_info "Bug名称: ${bug_name}"
    print_info "日期: ${date}"
    print_info "作者: ${author}"
    print_info "Commit: ${commit_hash}"
    echo ""

    # 创建目录
    local archive_path=$(create_archive_dirs "${date}" "${bug_name}")

    # 生成文档
    local doc_path=$(create_document "${date}" "${bug_name}" "${author}" "${commit_hash}" "${archive_path}")

    # 显示下一步操作
    show_next_steps "${doc_path}" "${archive_path}"
}

# 运行主函数
main "$@"
