#!/bin/bash

# check-duplicates.sh - 检查重复文档脚本
# 使用方法：./check-duplicates.sh
# 功能：检查飞书或本地是否有重复的文档

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\133[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}=== 检查重复文档 ===${NC}"
echo ""

# 检查本地重复文档
echo -e "${YELLOW}检查本地重复文档...${NC}"
echo ""

# 检查 current/ 目录
if [ -d "current/" ]; then
    # 检查大纲重复
    if [ -f "current/outline.md" ] && [ -f "current/32-chapters-outline-final.md" ]; then
        echo -e "${RED}发现重复大纲：${NC}"
        echo "  - current/outline.md"
        echo "  - current/32-chapters-outline-final.md"
        echo "  - 建议：保留 current/outline.md，删除或归档另一个"
        echo ""
    fi
    
    # 检查章节重复
    for chapter in current/chapter-*.md; do
        chapter_name=$(basename "$chapter" .md)
        # 检查是否有多个版本在 archive/
        count=$(find archive/chapter-history -name "$chapter_name*.md" 2>/dev/null | wc -l)
        if [ $count -gt 1 ]; then
            echo -e "${YELLOW}章节 $chapter_name 有 $count 个历史版本${NC}"
        fi
    done
fi

echo ""
echo -e "${YELLOW}检查完成！${NC}"
echo ""
echo -e "${GREEN}建议：${NC}"
echo "1. 删除重复的文档"
echo "2. 确保每个文档类型只有一个最新版本"
echo "3. 旧版本都归档到 archive/ 目录"
