#!/bin/bash

# AI Service 部署脚本

set -e

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

# 配置
SERVICE_NAME="ai-service"
SERVICE_PORT=8087
REGISTRY="registry.example.com"
TAG=${VERSION:-latest}

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}  AI Service 部署脚本${NC}"
echo -e "${GREEN}========================================${NC}"

# 检查环境
check_env() {
    echo -e "${YELLOW}[1/4] 检查环境...${NC}"
    
    if ! command -v docker &> /dev/null; then
        echo -e "${RED}错误: Docker 未安装${NC}"
        exit 1
    fi
    
    if ! command -v kubectl &> /dev/null; then
        echo -e "${YELLOW}警告: kubectl 未安装，K8s部署将被跳过${NC}"
    fi
    
    echo -e "${GREEN}环境检查完成${NC}"
}

# 构建镜像
build_image() {
    echo -e "${YELLOW}[2/4] 构建Docker镜像...${NC}"
    
    cd "$(dirname "$0")/.."
    
    docker build -t ${SERVICE_NAME}:${TAG} -f docker/Dockerfile .
    docker tag ${SERVICE_NAME}:${TAG} ${REGISTRY}/${SERVICE_NAME}:${TAG}
    
    echo -e "${GREEN}镜像构建完成: ${SERVICE_NAME}:${TAG}${NC}"
}

# Docker Compose 部署
deploy_docker() {
    echo -e "${YELLOW}[3/4] 使用Docker Compose部署...${NC}"
    
    cd "$(dirname "$0")/.."
    
    # 设置环境变量
    export OPENAI_API_KEY=${OPENAI_API_KEY:-}
    export ALIYUN_QWEN_API_KEY=${ALIYUN_QWEN_API_KEY:-}
    export ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
    
    docker-compose -f docker/docker-compose.yml up -d
    
    echo -e "${GREEN}Docker Compose部署完成${NC}"
    echo -e "${GREEN}服务地址: http://localhost:${SERVICE_PORT}${NC}"
}

# K8s 部署
deploy_k8s() {
    echo -e "${YELLOW}[4/4] 使用Kubernetes部署...${NC}"
    
    if ! command -v kubectl &> /dev/null; then
        echo -e "${YELLOW}跳过K8s部署${NC}"
        return
    fi
    
    cd "$(dirname "$0")/.."
    
    # 应用配置
    kubectl apply -f k8s/config.yaml
    kubectl apply -f k8s/deployment.yaml
    
    # 等待部署完成
    echo -e "${YELLOW}等待Pod启动...${NC}"
    kubectl rollout status deployment/ai-service -n erp
    
    echo -e "${GREEN}Kubernetes部署完成${NC}"
}

# 显示状态
show_status() {
    echo ""
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}  部署状态${NC}"
    echo -e "${GREEN}========================================${NC}"
    
    if command -v kubectl &> /dev/null; then
        kubectl get pods -l app=${SERVICE_NAME} -n erp 2>/dev/null || true
    fi
    
    echo ""
    echo -e "${GREEN}API端点:${NC}"
    echo -e "  - 健康检查: http://localhost:${SERVICE_PORT}/api/ai/health"
    echo -e "  - 对话接口: http://localhost:${SERVICE_PORT}/api/ai/chat"
    echo -e "  - 模型管理: http://localhost:${SERVICE_PORT}/api/ai/models"
    echo ""
}

# 主流程
main() {
    check_env
    
    case "${DEPLOY_MODE:-docker}" in
        docker)
            build_image
            deploy_docker
            ;;
        k8s)
            build_image
            deploy_k8s
            ;;
        all)
            build_image
            deploy_docker
            deploy_k8s
            ;;
        *)
            echo -e "${RED}未知的部署模式: ${DEPLOY_MODE}${NC}"
            echo "支持的模式: docker, k8s, all"
            exit 1
            ;;
    esac
    
    show_status
    
    echo -e "${GREEN}部署完成!${NC}"
}

# 卸载
uninstall() {
    echo -e "${YELLOW}卸载AI Service...${NC}"
    
    if command -v kubectl &> /dev/null; then
        kubectl delete -f k8s/deployment.yaml -n erp 2>/dev/null || true
        kubectl delete -f k8s/config.yaml -n erp 2>/dev/null || true
    fi
    
    cd "$(dirname "$0")/.." 2>/dev/null || true
    docker-compose -f docker/docker-compose.yml down 2>/dev/null || true
    
    echo -e "${GREEN}卸载完成${NC}"
}

# 根据参数执行
case "${1:-deploy}" in
    deploy)
        main
        ;;
    uninstall)
        uninstall
        ;;
    restart)
        docker-compose -f docker/docker-compose.yml restart ${SERVICE_NAME}
        ;;
    logs)
        docker-compose -f docker/docker-compose.yml logs -f ${SERVICE_NAME}
        ;;
    *)
        echo "用法: $0 {deploy|uninstall|restart|logs}"
        exit 1
        ;;
esac
