#!/bin/bash

# AI 面试系统部署脚本
# 使用方法: bash deploy.sh

set -e

echo "=========================================="
echo "AI 语音面试系统 - Docker 部署"
echo "=========================================="

# 配置
DEPLOY_DIR="/www/wwwroot/ai-interview"
DOMAIN="interview.test.ai.ireborn.com.cn"

# 检查 Docker
if ! command -v docker &> /dev/null; then
    echo "❌ Docker 未安装，请先安装 Docker"
    exit 1
fi

if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose 未安装，请先安装"
    exit 1
fi

echo "✅ Docker 环境检查通过"

# 创建部署目录
echo "📁 创建部署目录..."
mkdir -p $DEPLOY_DIR
cd $DEPLOY_DIR

# 检查 .env 文件
if [ ! -f "deploy/.env" ]; then
    echo "⚠️ 未找到 .env 文件，正在创建..."
    mkdir -p deploy
    cat > deploy/.env << 'EOF'
# Coze 配置
COZE_PAT_TOKEN=pat_nd1wU47WyPS9GCIyJ1clnH8h1WOQXGrYELX8w73TnSZaYbFdYD4swIhzcETBUbfT
COZE_BOT_ID=7595113005181386792

# 工作流 ID
COZE_WORKFLOW_A_ID=7597357422713798710
COZE_WORKFLOW_C_ID=7597376294612107318

# 文件服务器
FILE_SERVER_URL=https://files.test.ai.ireborn.com.cn
FILE_SERVER_TOKEN=ai_interview_2026_secret
EOF
    echo "✅ .env 文件已创建"
fi

# 构建并启动
echo "🐳 构建 Docker 镜像..."
cd deploy
docker-compose down 2>/dev/null || true
docker-compose up -d --build

echo "⏳ 等待服务启动..."
sleep 10

# 检查服务状态
echo "🔍 检查服务状态..."
docker-compose ps

echo ""
echo "=========================================="
echo "✅ 部署完成!"
echo "=========================================="
echo ""
echo "访问地址:"
echo "  用户端:    http://$DOMAIN"
echo "  管理后台:  http://$DOMAIN/admin"
echo "  后端 API:  http://$DOMAIN/api"
echo ""
echo "容器状态:"
docker-compose ps --format "table {{.Name}}\t{{.Status}}\t{{.Ports}}"
echo ""
echo "查看日志: docker-compose logs -f"
echo "=========================================="
