#!/bin/sh

# 定义颜色
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo "${BLUE}[信息] 正在检查 Docker 环境...${NC}"

# 检查是否安装了 Docker
if ! command -v docker &> /dev/null
then
    echo "${RED}[错误] 未找到 Docker，请先安装 Docker。${NC}"
    exit 1
fi

# 进入脚本所在目录
cd "$(dirname "$0")"

{% if action == 'start' %}
echo "${BLUE}[信息] 正在启动服务 (构建并后台运行)...${NC}"
docker-compose up -d --build

if [ $? -eq 0 ]; then
    echo ""
    echo "${GREEN}[成功] 服务启动成功！${NC}"
    echo "----------------------------------------------------"
    echo "${YELLOW}Web 应用地址${NC}:   http://localhost:8000"
    echo "${YELLOW}API 文档地址${NC}:   http://localhost:8000/docs"
    echo "${YELLOW}Redis 端口${NC}:     6379"
    echo "----------------------------------------------------"
    echo "${BLUE}[提示]${NC} 使用 ${YELLOW}docker-compose logs -f${NC} 查看实时日志。"
    echo "${BLUE}[提示]${NC} 使用 ${YELLOW}./stop_docker.sh${NC} 停止服务。"
else
    echo "${RED}[错误] 服务启动失败，请检查 Docker 日志。${NC}"
    exit 1
fi

{% elif action == 'stop' %}
echo "${BLUE}[信息] 正在停止并清理服务...${NC}"
docker-compose down

if [ $? -eq 0 ]; then
    echo "${GREEN}[成功] 服务已完全停止。${NC}"
else
    echo "${RED}[错误] 停止服务时遇到问题。${NC}"
    exit 1
fi

{% elif action == 'restart' %}
echo "${BLUE}[信息] 正在重启服务...${NC}"

echo "${BLUE}[1/2] 停止服务...${NC}"
docker-compose down

echo "${BLUE}[2/2] 重新启动服务...${NC}"
docker-compose up -d --build

if [ $? -eq 0 ]; then
    echo ""
    echo "${GREEN}[成功] 服务重启完成！${NC}"
    echo "----------------------------------------------------"
    echo "${YELLOW}Web 应用地址${NC}:   http://localhost:8000"
    echo "${YELLOW}API 文档地址${NC}:   http://localhost:8000/docs"
    echo "----------------------------------------------------"
else
    echo "${RED}[错误] 重启服务失败。${NC}"
    exit 1
fi
{% endif %}
