#!/bin/bash
# Paper Burner Local Proxy 一键部署脚本

set -e

cd "$(dirname "$0")"

echo "🚀 Paper Burner Local Proxy 部署脚本"
echo "======================================"

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

if ! docker info &> /dev/null; then
    echo "❌ Docker 未运行，请启动 Docker"
    exit 1
fi

echo "✅ Docker 可用"

# 启动 PostgreSQL
echo ""
echo "📦 启动 PostgreSQL..."
docker compose up -d

# 等待数据库就绪
echo "⏳ 等待数据库就绪..."
for i in {1..30}; do
    if docker exec paperburner-postgres pg_isready -U paperburner &> /dev/null; then
        echo "✅ PostgreSQL 已就绪"
        break
    fi
    sleep 1
done

# 运行数据库迁移
echo ""
echo "🔧 运行数据库迁移..."
npx prisma@5 migrate deploy

echo ""
echo "✅ 部署完成！"
echo ""
echo "数据库连接信息:"
echo "  Host: localhost:15432"
echo "  User: paperburner"
echo "  Password: paperburner123"
echo "  Database: paperburner"
echo ""
echo "启动服务器: npm start"
echo "停止数据库: docker compose down"