#!/bin/bash
# 多Agent协作文件监控脚本
# 用途：监控GDD文件变化，自动通知其他agent
# 使用：./WATCHDOG.sh

set -e

# 配置
WATCH_DIR="/Users/xuqinmin/Projects/NewsProject/lawless/docs/设计文档"
SYNC_FILE="/Users/xuqinmin/Projects/NewsProject/lawless/docs/ai-collab/SYNC.md"
LOG_FILE="/Users/xuqinmin/Projects/NewsProject/lawless/docs/ai-collab/watchdog.log"

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

# 日志函数
log() {
    echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

# 检查依赖
check_dependencies() {
    if ! command -v fswatch &> /dev/null; then
        echo -e "${RED}错误：fswatch 未安装${NC}"
        echo "请运行：brew install fswatch"
        exit 1
    fi
}

# 更新SYNC.md
update_sync() {
    local message="$1"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')

    # 在SYNC.md的"最近更新日志"部分添加新日志
    if grep -q "最近更新日志" "$SYNC_FILE"; then
        # 使用sed在"最近更新日志"后添加新行
        sed -i '' "/最近更新日志/a\\
\\
[${timestamp}] ${message}" "$SYNC_FILE"
    else
        echo -e "\n## 最近更新日志\n\n[${timestamp}] ${message}" >> "$SYNC_FILE"
    fi
}

# 监控文件变化
watch_files() {
    log "${GREEN}开始监控文件变化...${NC}"
    log "${YELLOW}监控目录：${WATCH_DIR}${NC}"
    log "${YELLOW}同步文件：${SYNC_FILE}${NC}"
    log "${YELLOW}日志文件：${LOG_FILE}${NC}"
    log ""

    # 使用fswatch监控文件变化
    fswatch -o "$WATCH_DIR" | while read f; do
        local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
        local changed_files=$(find "$WATCH_DIR" -name "*.md" -newer "$LOG_FILE" -type f 2>/dev/null)

        if [ -n "$changed_files" ]; then
            log "${GREEN}检测到文件变化：${NC}"
            echo "$changed_files" | while read file; do
                local filename=$(basename "$file")
                log "  - ${filename}"
                update_sync "📄 文件变化：${filename}"
            done
            log ""
        fi
    done
}

# 显示帮助
show_help() {
    echo "多Agent协作文件监控脚本"
    echo ""
    echo "用法："
    echo "  ./WATCHDOG.sh          # 开始监控"
    echo "  ./WATCHDOG.sh --help   # 显示帮助"
    echo "  ./WATCHDOG.sh --status # 显示当前状态"
    echo ""
    echo "功能："
    echo "  - 监控GDD文件变化"
    echo "  - 自动更新SYNC.md"
    echo "  - 记录变化日志"
    echo ""
    echo "依赖："
    echo "  - fswatch: brew install fswatch"
}

# 显示当前状态
show_status() {
    echo "当前监控状态："
    echo ""

    # 检查SYNC.md
    if [ -f "$SYNC_FILE" ]; then
        echo "SYNC.md 存在"
        echo "最近更新："
        tail -5 "$SYNC_FILE"
    else
        echo "SYNC.md 不存在"
    fi

    echo ""

    # 检查日志文件
    if [ -f "$LOG_FILE" ]; then
        echo "日志文件存在"
        echo "最近日志："
        tail -5 "$LOG_FILE"
    else
        echo "日志文件不存在"
    fi
}

# 主函数
main() {
    # 检查参数
    case "${1:-}" in
        --help|-h)
            show_help
            exit 0
            ;;
        --status|-s)
            show_status
            exit 0
            ;;
        "")
            # 继续执行
            ;;
        *)
            echo -e "${RED}未知参数：$1${NC}"
            show_help
            exit 1
            ;;
    esac

    # 检查依赖
    check_dependencies

    # 检查文件
    if [ ! -f "$SYNC_FILE" ]; then
        echo -e "${RED}错误：SYNC.md 不存在${NC}"
        echo "请先创建 SYNC.md 文件"
        exit 1
    fi

    # 开始监控
    watch_files
}

# 运行主函数
main "$@"
