#!/bin/bash

# AI 小说平台前端部署脚本 - Cloudflare Pages
# 使用方法: ./deploy-frontend.sh

echo "🚀 开始部署前端到 Cloudflare Pages..."

# 检查是否登录 Cloudflare
echo "📋 检查 Cloudflare 登录状态..."
if ! wrangler auth whoami &> /dev/null; then
    echo "❌ 未登录 Cloudflare，请先运行: wrangler login"
    exit 1
fi

# 安装依赖
echo "📦 安装前端依赖..."
npm ci

# 类型检查
echo "🔍 运行类型检查..."
npm run type-check

# 构建项目
echo "🔨 构建前端项目..."
npm run build

# 检查构建结果
if [ ! -d "dist" ]; then
    echo "❌ 构建失败：dist 目录不存在"
    exit 1
fi

echo "✅ 前端构建完成!"
echo ""
echo "📖 部署说明："
echo "1. 访问 Cloudflare Dashboard -> Pages -> 创建项目"
echo "2. 连接此 Git 仓库"
echo "3. 构建设置："
echo "   - 构建命令: npm run build"
echo "   - 构建目录: dist"
echo "4. 设置环境变量："
echo "   - VITE_API_BASE: https://your-worker.your-subdomain.workers.dev/api"
echo ""
echo "🎉 前端部署准备完成!"