#!/bin/bash
# 萌芽个人主页 - Docker 部署脚本 (Linux/Mac)

set -e

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


# 检查 Docker 是否安装
if ! command -v docker &> /dev/null; then
    echo -e "${RED}[ERROR]${NC} Docker 未安装"
    echo "请先安装 Docker: https://docs.docker.com/get-docker/"
    exit 1
fi

# 检查 Docker 是否运行
if ! docker ps &> /dev/null; then
    echo -e "${RED}[ERROR]${NC} Docker 服务未运行"
    echo "请启动 Docker 后重试"
    exit 1
fi

# 菜单函数
show_menu() {
    clear
    echo "========================================"
    echo "   萌芽个人主页 - Docker 部署菜单"
    echo "========================================"
    echo
    echo "[1] 构建并启动容器 (首次部署)"
    echo "[2] 启动容器"
    echo "[3] 停止容器"
    echo "[4] 重启容器"
    echo "[5] 查看日志"
    echo "[6] 查看容器状态"
    echo "[7] 停止并删除容器"
    echo "[8] 重新构建镜像"
    echo "[0] 退出"
    echo
}

# 构建并启动
build_and_start() {
    echo
    echo -e "${YELLOW}[INFO]${NC} 创建持久化目录..."
    mkdir -p /shumengya/docker/storage/mengyaprofile/data
    mkdir -p /shumengya/docker/storage/mengyaprofile/background
    
    echo -e "${YELLOW}[INFO]${NC} 复制初始数据..."
    if [ ! -f /shumengya/docker/storage/mengyaprofile/data/profile.json ]; then
        cp -r mengyaprofile-backend/data/* /shumengya/docker/storage/mengyaprofile/data/
    fi
    if [ -d mengyaprofile-frontend/public/background ] && [ "$(ls -A mengyaprofile-frontend/public/background)" ]; then
        cp -r mengyaprofile-frontend/public/background/* /shumengya/docker/storage/mengyaprofile/background/ 2>/dev/null || true
    fi
    
    echo -e "${YELLOW}[INFO]${NC} 正在构建并启动容器..."
    if docker-compose up -d --build; then
        echo
        echo -e "${GREEN}[SUCCESS]${NC} 启动成功!"
        echo "访问地址: http://localhost:5000"
        echo "管理员模式: http://localhost:5000/admin?token=shumengya520"
    else
        echo -e "${RED}[ERROR]${NC} 启动失败"
    fi
    echo
    read -p "按回车键继续..."
}

# 启动容器
start() {
    echo
    echo -e "${YELLOW}[INFO]${NC} 正在启动容器..."
    if docker-compose up -d; then
        echo -e "${GREEN}[SUCCESS]${NC} 启动成功!"
    else
        echo -e "${RED}[ERROR]${NC} 启动失败"
    fi
    read -p "按回车键继续..."
}

# 停止容器
stop() {
    echo
    echo -e "${YELLOW}[INFO]${NC} 正在停止容器..."
    docker-compose stop
    echo -e "${GREEN}[SUCCESS]${NC} 已停止"
    read -p "按回车键继续..."
}

# 重启容器
restart() {
    echo
    echo -e "${YELLOW}[INFO]${NC} 正在重启容器..."
    docker-compose restart
    echo -e "${GREEN}[SUCCESS]${NC} 已重启"
    read -p "按回车键继续..."
}

# 查看日志
logs() {
    echo
    echo -e "${YELLOW}[INFO]${NC} 显示日志 (按 Ctrl+C 退出)..."
    echo
    docker-compose logs -f
    read -p "按回车键继续..."
}

# 查看状态
status() {
    echo
    echo -e "${YELLOW}[INFO]${NC} 容器状态:"
    echo
    docker-compose ps
    echo
    echo -e "${YELLOW}[INFO]${NC} 资源占用:"
    docker stats --no-stream mengya-profile
    echo
    read -p "按回车键继续..."
}

# 删除容器
remove() {
    echo
    echo -e "${YELLOW}[WARNING]${NC} 将停止并删除容器(数据不会丢失)"
    read -p "确认删除? (y/n): " confirm
    if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
        echo
        echo -e "${YELLOW}[INFO]${NC} 正在删除容器..."
        docker-compose down
        echo -e "${GREEN}[SUCCESS]${NC} 已删除"
    fi
    read -p "按回车键继续..."
}

# 重新构建
rebuild() {
    echo
    echo -e "${YELLOW}[INFO]${NC} 正在重新构建镜像..."
    docker-compose down
    if docker-compose build --no-cache && docker-compose up -d; then
        echo -e "${GREEN}[SUCCESS]${NC} 重新构建完成!"
    else
        echo -e "${RED}[ERROR]${NC} 构建失败"
    fi
    read -p "按回车键继续..."
}

# 主循环
while true; do
    show_menu
    read -p "请选择操作 (0-8): " choice
    
    case $choice in
        1) build_and_start ;;
        2) start ;;
        3) stop ;;
        4) restart ;;
        5) logs ;;
        6) status ;;
        7) remove ;;
        8) rebuild ;;
        0) 
            echo
            echo "感谢使用萌芽个人主页 Docker 部署工具!"
            exit 0
            ;;
        *) 
            echo -e "${RED}无效选择${NC}"
            sleep 1
            ;;
    esac
done
