#!/bin/bash

# 小说创作 Git 工作流脚本
# 专门用于小说章节的版本管理

set -e

function show_usage() {
    echo "📚 小说创作 Git 工作流"
    echo ""
    echo "使用方法: $0 [命令]"
    echo ""
    echo "命令:"
    echo "  start <chapter-name>   开始新章节"
    echo "  save <chapter-num>     保存章节进度"
    echo "  finish <chapter-num>   完成章节并合并"
    echo "  backup                备份所有章节"
    echo "  restore <chapter-num> 恢复章节版本"
    echo "  status                显示章节状态"
    echo "  help                  显示帮助"
    echo ""
    echo "示例:"
    echo "  $0 start chapter-19"
    echo "  $0 save 19"
    echo "  $0 finish 19"
    echo "  $0 backup"
}

function start_chapter() {
    local chapter_name=$1
    
    if [ -z "$chapter_name" ]; then
        echo "❌ 请提供章节名称"
        show_usage
        exit 1
    fi
    
    echo "🚀 开始新章节: $chapter_name"
    
    # 切换到 develop 分支
    git checkout develop 2>/dev/null || git checkout -b develop
    
    # 创建功能分支
    local branch_name="feature/$chapter_name"
    git checkout -b "$branch_name" develop
    
    echo "✅ 已创建分支: $branch_name"
    echo ""
    echo "📝 下一步:"
    echo "1. 开始创作章节内容"
    echo "2. 使用 '$0 save' 保存进度"
    echo "3. 使用 '$0 finish' 完成章节"
}

function save_chapter() {
    local chapter_num=$1
    
    if [ -z "$chapter_num" ]; then
        echo "❌ 请提供章节编号"
        show_usage
        exit 1
    fi
    
    echo "💾 保存章节 $chapter_num 进度..."
    
    # 检查是否有未提交的变更
    if [ -z "$(git status --porcelain novel/)" ]; then
        echo "⚠️ 小说目录没有变更"
        return
    fi
    
    # 添加小说文件
    git add novel/
    
    # 提交
    local commit_msg="feat(chapter-$chapter_num): 保存进度 $(date '+%H:%M:%S')"
    git commit -m "$commit_msg"
    
    echo "✅ 进度已保存: $commit_msg"
    
    # 显示当前状态
    echo ""
    echo "📊 当前进度:"
    git log --oneline -3
}

function finish_chapter() {
    local chapter_num=$1
    
    if [ -z "$chapter_num" ]; then
        echo "❌ 请提供章节编号"
        show_usage
        exit 1
    fi
    
    echo "🎯 完成章节 $chapter_num..."
    
    # 确保在功能分支上
    local current_branch=$(git branch --show-current)
    if [[ ! "$current_branch" =~ ^feature/ ]]; then
        echo "❌ 当前不在功能分支上"
        echo "  当前分支: $current_branch"
        exit 1
    fi
    
    # 最后保存
    save_chapter "$chapter_num"
    
    # 切换到 develop 分支
    git checkout develop
    
    # 合并功能分支
    git merge --no-ff "$current_branch" -m "feat: 完成第 $chapter_num 章"
    
    # 删除功能分支
    git branch -d "$current_branch"
    
    echo "✅ 章节 $chapter_num 已完成并合并到 develop 分支"
    
    # 创建备份标签
    local tag_name="chapter-$chapter_num-$(date '+%Y%m%d-%H%M%S')"
    git tag -a "$tag_name" -m "章节 $chapter_num 完成备份"
    
    echo "🏷️ 已创建备份标签: $tag_name"
}

function backup_all() {
    echo "📦 备份所有章节..."
    
    # 确保在 develop 分支
    git checkout develop 2>/dev/null
    
    # 创建备份分支
    local backup_branch="backup/$(date '+%Y%m%d-%H%M%S')"
    git checkout -b "$backup_branch"
    
    # 提交当前状态
    git add .
    git commit -m "backup: 全量备份 $(date '+%Y-%m-%d %H:%M:%S')"
    
    # 创建标签
    local backup_tag="backup-$(date '+%Y%m%d-%H%M%S')"
    git tag -a "$backup_tag" -m "全量备份"
    
    # 切回原分支
    git checkout develop
    git branch -d "$backup_branch"
    
    echo "✅ 全量备份完成"
    echo "🏷️ 备份标签: $backup_tag"
}

function restore_chapter() {
    local chapter_num=$1
    
    if [ -z "$chapter_num" ]; then
        echo "❌ 请提供章节编号"
        show_usage
        exit 1
    fi
    
    echo "↩️ 恢复章节 $chapter_num..."
    
    # 查找相关标签
    local tags=$(git tag -l "chapter-$chapter_num-*" | sort -r | head -5)
    
    if [ -z "$tags" ]; then
        echo "❌ 未找到章节 $chapter_num 的备份"
        return
    fi
    
    echo "可用的版本:"
    select tag in $tags; do
        if [ -n "$tag" ]; then
            echo "恢复版本: $tag"
            
            # 创建恢复分支
            local restore_branch="restore/chapter-$chapter_num-$(date '+%Y%m%d-%H%M%S')"
            git checkout -b "$restore_branch" "$tag"
            
            echo "✅ 已创建恢复分支: $restore_branch"
            echo ""
            echo "📝 下一步:"
            echo "1. 检查恢复的内容"
            echo "2. 合并到当前分支: git merge $restore_branch"
            echo "3. 删除恢复分支: git branch -d $restore_branch"
            break
        fi
    done
}

function show_status() {
    echo "📊 小说创作状态"
    echo ""
    
    # 显示当前分支
    echo "🌿 当前分支: $(git branch --show-current)"
    echo ""
    
    # 显示未提交的变更
    echo "📝 未提交的变更:"
    git status --short novel/
    echo ""
    
    # 显示最近的章节提交
    echo "📅 最近的章节提交:"
    git log --oneline --grep="chapter-\|第.*章" -10
    echo ""
    
    # 显示章节标签
    echo "🏷️ 章节备份标签:"
    git tag -l "chapter-*" | sort -r | head -10
}

# 主程序
case "$1" in
    start)
        start_chapter "$2"
        ;;
    save)
        save_chapter "$2"
        ;;
    finish)
        finish_chapter "$2"
        ;;
    backup)
        backup_all
        ;;
    restore)
        restore_chapter "$2"
        ;;
    status)
        show_status
        ;;
    help|--help|-h)
        show_usage
        ;;
    *)
        echo "❌ 未知命令: $1"
        show_usage
        exit 1
        ;;
esac