#!/bin/bash
#
# Author: Emotion Museum Team
# Created: 2026-03-18
# Purpose: 一键部署域名配置（SSL + Nginx + 后端 + 前端）
#
# 使用方法:
#   ./deploy-domain.sh
#
# 前置条件:
#   - 已配置 SSH 免密登录到服务器
#   - 服务器已安装 certbot 和 nginx
#

set -e

# 配置
SERVER="101.200.208.45"
DOMAIN="lifescript.happylifeos.com"
EMAIL="admin@happylifeos.com"

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

log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_section() { echo -e "\n${BLUE}==== $1 ====${NC}\n"; }

log_section "情绪博物馆 - 域名部署"

# 1. 检查 SSH 连接
log_info "检查 SSH 连接..."
ssh root@$SERVER "echo 'SSH 连接正常'" || { log_error "SSH 连接失败"; exit 1; }

# 2. 上传 SSL 证书脚本
log_info "上传 SSL 证书脚本..."
scp tools/deploy-ssl-cert.py root@$SERVER:/tmp/deploy-ssl-cert.py

# 3. 执行 SSL 证书验证（证书应该已申请）
log_info "验证 SSL 证书..."
ssh root@$SERVER "python3 /tmp/deploy-ssl-cert.py --verify" || log_warn "证书验证失败，请检查"

# 4. 上传并应用 Nginx 配置
log_info "上传 Nginx 配置..."
scp conf/nginx-emotion-museum-fix.conf root@$SERVER:/tmp/nginx-fix.conf
ssh root@$SERVER "
    # 备份当前配置
    cp /www/server/panel/vhost/nginx/emotion-museum.conf /www/server/panel/vhost/nginx/emotion-museum.conf.bak 2>/dev/null || true
    # 应用新配置
    cp /tmp/nginx-fix.conf /www/server/panel/vhost/nginx/emotion-museum.conf
    # 验证并重载
    /www/server/nginx/sbin/nginx -t && /www/server/nginx/sbin/nginx -s reload
"
log_info "Nginx 配置已应用"

# 5. 部署后端
log_info "部署后端服务..."
cd backend-single && mvn clean package -DskipTests && cd ..
scp backend-single/target/backend-single-1.0.0.jar root@$SERVER:/opt/emotion-museum/backend.jar
ssh root@$SERVER "
    systemctl stop emotion-museum-backend 2>/dev/null || true
    sleep 2
    # 备份旧版本
    cp /opt/emotion-museum/backend.jar /opt/emotion-museum/backend.jar.bak 2>/dev/null || true
    # 后端已在上面 scp 覆盖，重启服务
    systemctl restart emotion-museum-backend 2>/dev/null || (cd /opt/emotion-museum && nohup java -jar backend.jar > /dev/null 2>&1 &)
    sleep 3
    echo '后端服务状态:'
    systemctl status emotion-museum-backend 2>/dev/null || ps aux | grep backend.jar | grep -v grep
"

# 6. 部署前端
log_info "部署前端..."
cd web && npm run build && cd ..
scp -r web/dist/* root@$SERVER:/data/www/emotion-museum/
log_info "前端部署完成"

# 7. 部署管理后台
log_info "部署管理后台..."
cd web-admin && npm run build && cd ..
scp -r web-admin/dist/* root@$SERVER:/data/www/emotion-museum-admin/
log_info "管理后台部署完成"

# 8. 验证部署
log_section "验证部署"
log_info "验证 HTTPS 访问..."
curl -k -s -o /dev/null -w "  前端页面：HTTP %{http_code}\n" https://$DOMAIN/emotion-museum/
curl -k -s -o /dev/null -w "  管理后台：HTTP %{http_code}\n" https://$DOMAIN/emotion-museum-admin/
curl -k -s -o /dev/null -w "  API 代理：HTTP %{http_code}\n" https://$DOMAIN/api/health
curl -s -o /dev/null -w "  HTTP 跳转：HTTP %{http_code}\n" http://$DOMAIN/

log_section "部署完成"
log_info "访问地址:"
log_info "  用户前端：https://$DOMAIN/emotion-museum/"
log_info "  管理后台：https://$DOMAIN/emotion-museum-admin/"
log_info "  API 地址：https://$DOMAIN/api/"
log_info "  WebSocket: wss://$DOMAIN/ws"
