#!/bin/bash

set -e

echo "🚀 快速部署 Filament Admin"

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

# 检查镜像是否存在
if ! docker images filamentadmin:latest | grep -q filamentadmin; then
    echo -e "${RED}❌ 错误：未找到镜像 filamentadmin:latest${NC}"
    echo -e "${YELLOW}💡 请先运行 ./build.sh 构建镜像${NC}"
    exit 1
fi

echo -e "${BLUE}ℹ️  部署信息：${NC}"
echo "   镜像：filamentadmin:latest"
echo "   端口：5019:8000"
echo "   容器名：filament_admin"
echo ""

# 停止并删除现有容器
if docker ps -a | grep -q filament_admin; then
    echo -e "${YELLOW}🛑 停止现有容器...${NC}"
    docker compose down
fi

# 启动新容器
echo -e "${GREEN}▶️  启动容器...${NC}"
docker compose up -d

# 等待服务启动
echo -e "${YELLOW}⏳ 等待服务启动...${NC}"
sleep 5

# 检查状态
if docker compose ps | grep -q "Up"; then
    echo -e "${GREEN}✅ 部署成功！${NC}"
    echo ""
    echo -e "${BLUE}🌐 访问地址：${NC}"
    echo -e "   http://localhost:5019/admin"
    echo ""
    echo -e "${BLUE}📋 常用命令：${NC}"
    echo -e "   查看日志：${GREEN}docker compose logs -f${NC}"
    echo -e "   停止服务：${GREEN}docker compose down${NC}"
    echo -e "   重启服务：${GREEN}docker compose restart${NC}"
else
    echo -e "${RED}❌ 部署失败，请检查日志：${NC}"
    docker compose logs
    exit 1
fi
