#!/bin/bash
# 停止 Identity Service

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

echo -e "${YELLOW}🛑 停止 Identity Service...${NC}"

# 查找监听 3000 端口的进程
PID=$(lsof -ti :3000)

if [ -z "$PID" ]; then
    echo -e "${YELLOW}⚠️  Identity Service 未在运行${NC}"
    exit 0
fi

echo "找到进程: PID=$PID"

# 尝试优雅停止
echo "发送 SIGTERM 信号..."
kill $PID

# 等待进程结束
for i in {1..10}; do
    if ! kill -0 $PID 2>/dev/null; then
        echo -e "${GREEN}✓ Identity Service 已停止${NC}"
        exit 0
    fi
    sleep 1
    echo -n "."
done

echo ""
echo -e "${YELLOW}⚠️  进程未响应，强制停止...${NC}"
kill -9 $PID

if ! kill -0 $PID 2>/dev/null; then
    echo -e "${GREEN}✓ Identity Service 已强制停止${NC}"
else
    echo -e "${RED}✗ 无法停止进程${NC}"
    exit 1
fi
