#!/bin/bash

# =============================================
# 资产管理模块部署脚本 - 方案 2
# 创建独立的 oneos_asset 数据库
# =============================================

set -e  # 遇到错误立即退出

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

echo "=========================================="
echo "资产管理模块部署 - 方案 2"
echo "=========================================="
echo "时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo ""

# 配置
SERVER_IP="47.103.115.36"
SERVER_USER="root"
SERVER_PASSWORD="Passw0rd2026@ssl"
MYSQL_PASSWORD="Passw0rd2026"
DATABASE_NAME="oneos_asset"
SQL_FILE="sql/update-2026-03-12-停车场和车型参数模块.sql"

echo "=== 步骤 1：准备 SQL 文件 ==="
if [ ! -f "$SQL_FILE" ]; then
    echo -e "${RED}❌ SQL 文件不存在: $SQL_FILE${NC}"
    exit 1
fi
echo -e "${GREEN}✅ SQL 文件存在${NC}"
echo ""

echo "=== 步骤 2：上传 SQL 文件到服务器 ==="
echo "提示：需要手动输入密码: $SERVER_PASSWORD"
scp "$SQL_FILE" ${SERVER_USER}@${SERVER_IP}:/tmp/update-asset.sql
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ SQL 文件上传成功${NC}"
else
    echo -e "${RED}❌ SQL 文件上传失败${NC}"
    exit 1
fi
echo ""

echo "=== 步骤 3：上传 Nacos 配置文件 ==="
echo "提示：需要手动输入密码: $SERVER_PASSWORD"
scp nacos-config/asset-server-dev.yaml ${SERVER_USER}@${SERVER_IP}:/tmp/
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 配置文件上传成功${NC}"
else
    echo -e "${RED}❌ 配置文件上传失败${NC}"
    exit 1
fi
echo ""

echo "=== 步骤 4：在服务器上执行部署 ==="
echo "提示：需要手动输入密码: $SERVER_PASSWORD"
ssh ${SERVER_USER}@${SERVER_IP} << 'ENDSSH'

echo "=========================================="
echo "在服务器上执行部署操作"
echo "=========================================="

# 检查 MySQL 容器
echo ""
echo "=== 4.1 检查 MySQL 容器 ==="
if docker ps | grep -q oneos-mysql; then
    echo "✅ MySQL 容器运行中"
else
    echo "❌ MySQL 容器未运行"
    exit 1
fi

# 创建数据库
echo ""
echo "=== 4.2 创建 oneos_asset 数据库 ==="
docker exec oneos-mysql mysql -uroot -pPassw0rd2026 -e "CREATE DATABASE IF NOT EXISTS oneos_asset DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" 2>&1 | grep -v "Warning"
if [ $? -eq 0 ]; then
    echo "✅ 数据库创建成功"
else
    echo "❌ 数据库创建失败"
    exit 1
fi

# 验证数据库
echo ""
echo "=== 4.3 验证数据库 ==="
docker exec oneos-mysql mysql -uroot -pPassw0rd2026 -e "SHOW DATABASES LIKE 'oneos_asset';" 2>&1 | grep -v "Warning"

# 执行 SQL 脚本
echo ""
echo "=== 4.4 执行 SQL 脚本 ==="
docker exec -i oneos-mysql mysql -uroot -pPassw0rd2026 oneos_asset < /tmp/update-asset.sql 2>&1 | grep -v "Warning"
if [ $? -eq 0 ]; then
    echo "✅ SQL 脚本执行成功"
else
    echo "❌ SQL 脚本执行失败"
    exit 1
fi

# 验证表结构
echo ""
echo "=== 4.5 验证表结构 ==="
echo "查看创建的表："
docker exec oneos-mysql mysql -uroot -pPassw0rd2026 oneos_asset -e "SHOW TABLES LIKE 'asset_%';" 2>&1 | grep -v "Warning"

echo ""
echo "停车场表结构："
docker exec oneos-mysql mysql -uroot -pPassw0rd2026 oneos_asset -e "DESC asset_parking;" 2>&1 | grep -v "Warning" | head -15

echo ""
echo "车型参数表结构："
docker exec oneos-mysql mysql -uroot -pPassw0rd2026 oneos_asset -e "DESC asset_vehicle_model;" 2>&1 | grep -v "Warning" | head -15

echo ""
echo "车型维保项目关联表结构："
docker exec oneos-mysql mysql -uroot -pPassw0rd2026 oneos_asset -e "DESC asset_vehicle_model_maintain_item;" 2>&1 | grep -v "Warning" | head -15

# 检查 Nacos 容器
echo ""
echo "=== 4.6 检查 Nacos 容器 ==="
if docker ps | grep -q nacos; then
    echo "✅ Nacos 容器运行中"
    NACOS_CONTAINER=$(docker ps | grep nacos | awk '{print $1}')
    echo "Nacos 容器 ID: $NACOS_CONTAINER"
    
    # 复制配置文件到 Nacos 容器
    echo ""
    echo "=== 4.7 上传配置到 Nacos ==="
    echo "提示：需要手动通过 Nacos 控制台上传配置文件"
    echo "配置文件位置: /tmp/asset-server-dev.yaml"
    echo "Nacos 地址: http://47.103.115.36:8848/nacos"
    echo "命名空间: dev"
    echo "Data ID: asset-server-dev.yaml"
    echo "Group: DEFAULT_GROUP"
else
    echo "⚠️  Nacos 容器未运行，需要手动配置"
fi

echo ""
echo "=========================================="
echo "服务器端部署完成"
echo "=========================================="

ENDSSH

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ 服务器端部署成功${NC}"
else
    echo -e "${RED}❌ 服务器端部署失败${NC}"
    exit 1
fi

echo ""
echo "=========================================="
echo "部署总结"
echo "=========================================="
echo -e "${GREEN}✅ 数据库创建成功: oneos_asset${NC}"
echo -e "${GREEN}✅ SQL 脚本执行成功${NC}"
echo -e "${GREEN}✅ 表结构验证通过${NC}"
echo ""
echo "⚠️  下一步操作："
echo "1. 通过 Nacos 控制台上传配置文件"
echo "   - 访问: http://47.103.115.36:8848/nacos"
echo "   - 命名空间: dev"
echo "   - Data ID: asset-server-dev.yaml"
echo "   - Group: DEFAULT_GROUP"
echo "   - 配置内容: nacos-config/asset-server-dev.yaml"
echo ""
echo "2. 启动资产管理服务"
echo "   cd /Users/kkfluous/Projects/ai-coding/ln-oneos/oneos-backend"
echo "   mvn spring-boot:run"
echo ""
echo "3. 执行 API 测试"
echo "   ./test-api.sh"
echo ""
echo "=========================================="
echo "部署完成时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "=========================================="
