#!/bin/bash

# RTSP视频流管理系统 - Docker停止脚本

set -e

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

print_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

echo ""
print_info "=========================================="
print_info "停止RTSP视频流管理系统"
print_info "=========================================="
echo ""

# 检查Docker Compose
if command -v docker-compose &> /dev/null || docker compose version &> /dev/null 2>&1; then
    print_info "使用Docker Compose停止服务..."
    
    if command -v docker-compose &> /dev/null; then
        docker-compose down
    else
        docker compose down
    fi
    
    print_info "✅ 服务已停止"
else
    print_info "使用Docker命令停止服务..."
    
    # 检查容器是否存在
    if docker ps -a | grep -q jtjai_media; then
        docker stop jtjai_media
        print_info "✅ 容器已停止"
        
        # 询问是否删除容器
        read -p "是否删除容器? (y/n) " -n 1 -r
        echo ""
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            docker rm jtjai_media
            print_info "✅ 容器已删除"
        fi
    else
        print_warn "未找到运行的容器"
    fi
fi

echo ""
print_info "=========================================="
print_info "服务已停止"
print_info "=========================================="
echo ""
