#!/bin/bash
# Author: huazhongmin
# Created: 2026-03-17
# Purpose: 域名一键部署脚本 - 支持 SSL 证书申请、前端、后端、nginx 配置部署

# 情绪博物馆 - 域名部署脚本
# 域名：lifescript.happylifeos.com
# 使用方法：bash deploy-to-prod.sh [ssl|frontend|admin|life-script|backend|nginx|all]

set -e

# 颜色定义
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 ""
    echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}"
    echo -e "${BLUE}║${NC} $1"
    echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}"
    echo ""
}

# 服务器配置
SERVER_IP="101.200.208.45"
USERNAME="root"
DOMAIN="lifescript.happylifeos.com"

# SSL 证书申请
deploy_ssl() {
    log_section "申请 SSL 证书"

    if [ ! -f "tools/deploy-ssl-cert.py" ]; then
        log_error "SSL 证书脚本不存在：tools/deploy-ssl-cert.py"
        return 1
    fi

    log_info "在服务器上执行 SSL 证书申请..."
    scp tools/deploy-ssl-cert.py ${USERNAME}@${SERVER_IP}:/tmp/deploy-ssl-cert.py
    ssh ${USERNAME}@${SERVER_IP} "python3 /tmp/deploy-ssl-cert.py"

    if [ $? -eq 0 ]; then
        log_info "✅ SSL 证书申请完成"
        return 0
    else
        log_error "❌ SSL 证书申请失败"
        return 1
    fi
}

# 部署后端
deploy_backend() {
    log_section "部署后端服务"

    if [ ! -f "backend-single/deploy.sh" ]; then
        log_error "后端部署脚本不存在"
        return 1
    fi

    log_info "执行后端部署..."
    cd backend-single
    bash deploy.sh remote
    local result=$?
    cd ..

    if [ $result -eq 0 ]; then
        log_info "✅ 后端部署完成"
        return 0
    else
        log_error "❌ 后端部署失败"
        return 1
    fi
}

# 部署前端
deploy_frontend() {
    log_section "部署用户前端"

    if [ ! -f "web/deploy.sh" ]; then
        log_error "前端部署脚本不存在"
        return 1
    fi

    log_info "执行前端部署..."
    cd web
    bash deploy.sh
    local result=$?
    cd ..

    if [ $result -eq 0 ]; then
        log_info "✅ 前端部署完成"
        return 0
    else
        log_error "❌ 前端部署失败"
        return 1
    fi
}

# 部署管理后台
deploy_admin() {
    log_section "部署管理后台"

    if [ ! -f "web-admin/deploy.sh" ]; then
        log_error "管理后台部署脚本不存在"
        return 1
    fi

    log_info "执行管理后台部署..."
    cd web-admin
    bash deploy.sh
    local result=$?
    cd ..

    if [ $result -eq 0 ]; then
        log_info "✅ 管理后台部署完成"
        return 0
    else
        log_error "❌ 管理后台部署失败"
        return 1
    fi
}

# 部署 Life-Script
deploy_life_script() {
    log_section "部署 Life-Script"

    if [ ! -f "life-script/deploy.sh" ]; then
        log_error "Life-Script 部署脚本不存在"
        return 1
    fi

    log_info "执行 Life-Script 部署..."
    cd life-script
    bash deploy.sh
    local result=$?
    cd ..

    if [ $result -eq 0 ]; then
        log_info "✅ Life-Script 部署完成"
        return 0
    else
        log_error "❌ Life-Script 部署失败"
        return 1
    fi
}

# 部署 Nginx 配置
deploy_nginx() {
    log_section "部署 Nginx 配置"

    if [ ! -f "conf/emotion-museum.conf" ]; then
        log_error "Nginx 配置文件不存在"
        return 1
    fi

    log_info "上传 Nginx 配置文件..."
    scp conf/emotion-museum.conf ${USERNAME}@${SERVER_IP}:/etc/nginx/sites-available/${DOMAIN}.conf

    log_info "启用站点配置..."
    ssh ${USERNAME}@${SERVER_IP} "ln -snf /etc/nginx/sites-available/${DOMAIN}.conf /etc/nginx/sites-enabled/${DOMAIN}.conf"

    log_info "移除默认配置（如果存在冲突）..."
    ssh ${USERNAME}@${SERVER_IP} "rm -f /etc/nginx/sites-enabled/default"

    log_info "验证 Nginx 配置..."
    if ssh ${USERNAME}@${SERVER_IP} "nginx -t"; then
        log_info "Nginx 配置验证通过"
        ssh ${USERNAME}@${SERVER_IP} "systemctl reload nginx"
        log_info "✅ Nginx 配置重载完成"
        return 0
    else
        log_error "❌ Nginx 配置验证失败"
        return 1
    fi
}

# 主程序
main() {
    DEPLOY_TYPE="${1:-all}"

    log_section "情绪博物馆 - 域名部署"
    log_info "域名：https://${DOMAIN}"
    log_info "部署类型：${DEPLOY_TYPE}"
    log_info "部署时间：$(date '+%Y-%m-%d %H:%M:%S')"

    case "$DEPLOY_TYPE" in
        ssl)
            deploy_ssl
            ;;
        backend)
            deploy_backend
            ;;
        frontend)
            deploy_frontend
            ;;
        admin)
            deploy_admin
            ;;
        life-script)
            deploy_life_script
            ;;
        nginx)
            deploy_nginx
            ;;
        all)
            deploy_ssl
            deploy_nginx
            deploy_backend
            deploy_frontend
            deploy_admin
            deploy_life_script
            ;;
        *)
            log_error "无效的部署类型：$DEPLOY_TYPE"
            echo "使用方法：bash deploy-to-prod.sh [ssl|backend|frontend|admin|life-script|nginx|all]"
            exit 1
            ;;
    esac

    log_section "部署完成"

    if [ "$DEPLOY_TYPE" = "all" ] || [ "$DEPLOY_TYPE" = "ssl" ]; then
        log_info "📋 验证 SSL 证书：运行 python3 tools/deploy-ssl-cert.py --verify"
    fi

    if [ "$DEPLOY_TYPE" = "all" ] || [ "$DEPLOY_TYPE" = "nginx" ]; then
        log_info "🌐 访问地址:"
        log_info "   用户前端：https://${DOMAIN}/"
        log_info "   管理后台：https://${DOMAIN}/emotion-museum-admin/"
        log_info "   Life-Script: https://${DOMAIN}/life-script/"
        log_info "   API 地址：https://${DOMAIN}/api"
        log_info "   WebSocket: wss://${DOMAIN}/ws"
    fi
}

main "$@"
