#!/bin/bash
# TSP智能助手Docker部署脚本

set -e

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

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

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

# 检查Docker和Docker Compose
check_dependencies() {
    log_info "检查依赖..."
    
    if ! command -v docker &> /dev/null; then
        log_error "Docker未安装，请先安装Docker"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        log_error "Docker Compose未安装，请先安装Docker Compose"
        exit 1
    fi
    
    log_success "依赖检查通过"
}

# 创建必要的目录
create_directories() {
    log_info "创建必要的目录..."
    
    mkdir -p logs/nginx
    mkdir -p monitoring/grafana/provisioning/datasources
    mkdir -p monitoring/grafana/provisioning/dashboards
    mkdir -p ssl
    mkdir -p data
    mkdir -p backups
    mkdir -p uploads
    mkdir -p config
    
    log_success "目录创建完成"
}

# 构建镜像
build_images() {
    log_info "构建Docker镜像..."
    
    # 构建主应用镜像
    docker-compose build --no-cache tsp-assistant
    
    log_success "镜像构建完成"
}

# 启动服务
start_services() {
    log_info "启动服务..."
    
    # 启动基础服务（MySQL, Redis）
    docker-compose up -d mysql redis
    
    # 等待数据库启动
    log_info "等待数据库启动..."
    sleep 30
    
    # 启动主应用
    docker-compose up -d tsp-assistant
    
    # 启动其他服务
    docker-compose up -d nginx prometheus grafana
    
    log_success "服务启动完成"
}

# 检查服务状态
check_services() {
    log_info "检查服务状态..."
    
    sleep 10
    
    # 检查主应用
    if curl -f http://localhost:5000/api/health &> /dev/null; then
        log_success "TSP助手服务正常"
    else
        log_warning "TSP助手服务可能未完全启动"
    fi
    
    # 检查Nginx
    if curl -f http://localhost/health &> /dev/null; then
        log_success "Nginx服务正常"
    else
        log_warning "Nginx服务可能未完全启动"
    fi
    
    # 检查Prometheus
    if curl -f http://localhost:9090 &> /dev/null; then
        log_success "Prometheus服务正常"
    else
        log_warning "Prometheus服务可能未完全启动"
    fi
    
    # 检查Grafana
    if curl -f http://localhost:3000 &> /dev/null; then
        log_success "Grafana服务正常"
    else
        log_warning "Grafana服务可能未完全启动"
    fi
}

# 显示服务信息
show_info() {
    log_info "服务访问信息："
    echo "  TSP助手: http://localhost:5000"
    echo "  Nginx代理: http://localhost"
    echo "  Prometheus: http://localhost:9090"
    echo "  Grafana: http://localhost:3000 (admin/admin123456)"
    echo "  MySQL: localhost:3306 (root/root123456)"
    echo "  Redis: localhost:6379 (密码: redis123456)"
    echo ""
    log_info "查看日志命令："
    echo "  docker-compose logs -f tsp-assistant"
    echo "  docker-compose logs -f mysql"
    echo "  docker-compose logs -f redis"
    echo "  docker-compose logs -f nginx"
}

# 停止服务
stop_services() {
    log_info "停止服务..."
    docker-compose down
    log_success "服务已停止"
}

# 清理资源
cleanup() {
    log_info "清理Docker资源..."
    docker system prune -f
    log_success "清理完成"
}

# 主函数
main() {
    case "${1:-start}" in
        "start")
            check_dependencies
            create_directories
            build_images
            start_services
            check_services
            show_info
            ;;
        "stop")
            stop_services
            ;;
        "restart")
            stop_services
            sleep 5
            start_services
            check_services
            show_info
            ;;
        "cleanup")
            stop_services
            cleanup
            ;;
        "logs")
            docker-compose logs -f "${2:-tsp-assistant}"
            ;;
        "status")
            docker-compose ps
            ;;
        *)
            echo "用法: $0 {start|stop|restart|cleanup|logs|status}"
            echo "  start   - 启动所有服务"
            echo "  stop    - 停止所有服务"
            echo "  restart - 重启所有服务"
            echo "  cleanup - 清理Docker资源"
            echo "  logs    - 查看日志 (可选指定服务名)"
            echo "  status  - 查看服务状态"
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"
