#!/bin/bash
# Docker Bot管理脚本
# 快速启动、停止、重启所有或单个bot

set -e

# Bot列表
BOTS=("tomato" "cyrene" "gold-ship" "lunatalk_private")

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

# 显示帮助信息
show_help() {
    cat << EOF
📦 Docker Bot 管理脚本

用法: $0 <command> [bot_name]

命令:
  start <bot>     启动指定bot (或all启动所有)
  stop <bot>      停止指定bot (或all停止所有)
  restart <bot>   重启指定bot (或all重启所有)
  logs <bot>      查看指定bot日志
  status          查看所有bot状态
  build           重新构建镜像
  clean           清理停止的容器和未使用的卷

可用的bot:
  - tomato          西红柿bot
  - cyrene          Cyrene bot
  - gold-ship       黄金船bot
  - lunatalk_private LunaTalk Private bot
  - all             所有bot

示例:
  $0 start tomato              启动tomato bot
  $0 stop all                  停止所有bot
  $0 restart cyrene            重启cyrene bot
  $0 logs tomato               查看tomato日志
  $0 status                    查看所有bot状态

EOF
}

# 检查bot是否有效
check_bot() {
    local bot=$1
    if [[ "$bot" == "all" ]]; then
        return 0
    fi
    for b in "${BOTS[@]}"; do
        if [[ "$b" == "$bot" ]]; then
            return 0
        fi
    done
    return 1
}

# 启动bot
start_bot() {
    local bot=$1
    if [[ "$bot" == "all" ]]; then
        echo -e "${GREEN}🚀 启动所有bot...${NC}"
        for b in "${BOTS[@]}"; do
            start_single_bot "$b"
        done
    else
        start_single_bot "$bot"
    fi
}

start_single_bot() {
    local bot=$1
    echo -e "${GREEN}🚀 启动 ${bot} bot...${NC}"
    cd "run/$bot"

    if [[ ! -f ".env" ]]; then
        echo -e "${RED}❌ 错误: .env 文件不存在${NC}"
        echo -e "${YELLOW}   请先复制 .env.example 为 .env 并配置${NC}"
        cd ../..
        return 1
    fi

    docker-compose up -d
    cd ../..
    echo -e "${GREEN}✅ ${bot} bot 启动完成${NC}"
}

# 停止bot
stop_bot() {
    local bot=$1
    if [[ "$bot" == "all" ]]; then
        echo -e "${YELLOW}🛑 停止所有bot...${NC}"
        for b in "${BOTS[@]}"; do
            stop_single_bot "$b"
        done
    else
        stop_single_bot "$bot"
    fi
}

stop_single_bot() {
    local bot=$1
    echo -e "${YELLOW}🛑 停止 ${bot} bot...${NC}"
    cd "run/$bot"
    docker-compose down
    cd ../..
    echo -e "${GREEN}✅ ${bot} bot 已停止${NC}"
}

# 重启bot
restart_bot() {
    local bot=$1
    if [[ "$bot" == "all" ]]; then
        echo -e "${YELLOW}🔄 重启所有bot...${NC}"
        for b in "${BOTS[@]}"; do
            restart_single_bot "$b"
        done
    else
        restart_single_bot "$bot"
    fi
}

restart_single_bot() {
    local bot=$1
    echo -e "${YELLOW}🔄 重启 ${bot} bot...${NC}"
    cd "run/$bot"
    docker-compose restart
    cd ../..
    echo -e "${GREEN}✅ ${bot} bot 重启完成${NC}"
}

# 查看日志
show_logs() {
    local bot=$1
    echo -e "${GREEN}📋 查看 ${bot} bot 日志...${NC}"
    cd "run/$bot"
    docker-compose logs -f
    cd ../..
}

# 查看状态
show_status() {
    echo -e "${GREEN}📊 Bot状态:${NC}"
    echo ""
    for bot in "${BOTS[@]}"; do
        cd "run/$bot"
        container_name="claude-bot-${bot//_/-}"
        if docker ps --format '{{.Names}}' | grep -q "^${container_name}$"; then
            echo -e "  ${GREEN}●${NC} ${bot}: 运行中"
        else
            echo -e "  ${RED}○${NC} ${bot}: 已停止"
        fi
        cd ../..
    done
    echo ""
}

# 构建镜像
build_image() {
    echo -e "${GREEN}🔨 构建Docker镜像...${NC}"
    docker build -t claude-bot:latest .
    echo -e "${GREEN}✅ 镜像构建完成${NC}"
}

# 清理
clean_docker() {
    echo -e "${YELLOW}🧹 清理Docker资源...${NC}"
    docker system prune -f
    echo -e "${GREEN}✅ 清理完成${NC}"
}

# 主逻辑
if [[ $# -lt 1 ]]; then
    show_help
    exit 0
fi

command=$1
bot=${2:-}

case "$command" in
    start)
        if [[ -z "$bot" ]]; then
            echo -e "${RED}❌ 错误: 请指定bot名称或使用 'all'${NC}"
            exit 1
        fi
        if ! check_bot "$bot"; then
            echo -e "${RED}❌ 错误: 未知的bot: $bot${NC}"
            exit 1
        fi
        start_bot "$bot"
        ;;
    stop)
        if [[ -z "$bot" ]]; then
            echo -e "${RED}❌ 错误: 请指定bot名称或使用 'all'${NC}"
            exit 1
        fi
        if ! check_bot "$bot"; then
            echo -e "${RED}❌ 错误: 未知的bot: $bot${NC}"
            exit 1
        fi
        stop_bot "$bot"
        ;;
    restart)
        if [[ -z "$bot" ]]; then
            echo -e "${RED}❌ 错误: 请指定bot名称或使用 'all'${NC}"
            exit 1
        fi
        if ! check_bot "$bot"; then
            echo -e "${RED}❌ 错误: 未知的bot: $bot${NC}"
            exit 1
        fi
        restart_bot "$bot"
        ;;
    logs)
        if [[ -z "$bot" ]]; then
            echo -e "${RED}❌ 错误: 请指定bot名称${NC}"
            exit 1
        fi
        if ! check_bot "$bot"; then
            echo -e "${RED}❌ 错误: 未知的bot: $bot${NC}"
            exit 1
        fi
        show_logs "$bot"
        ;;
    status)
        show_status
        ;;
    build)
        build_image
        ;;
    clean)
        clean_docker
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        echo -e "${RED}❌ 错误: 未知命令: $command${NC}"
        echo ""
        show_help
        exit 1
        ;;
esac
