#!/bin/bash
# HZHub AI 后端服务状态检查脚本
# 功能：查看服务运行状态

PROJECT_NAME="hzhub-ai"
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
PID_FILE="$PROJECT_DIR/.pid"
LOG_FILE="$PROJECT_DIR/logs/backend.log"

echo "========================================="
echo " $PROJECT_NAME 服务状态"
echo "========================================="

# 检查PID文件
if [ ! -f "$PID_FILE" ]; then
    echo "状态: ⚪ 未运行"
    exit 0
fi

PID=$(cat "$PID_FILE")

# 检查进程是否存在
if ps -p "$PID" > /dev/null 2>&1; then
    echo "状态: 🟢 运行中"
    echo "PID: $PID"

    # 显示进程详细信息
    ps -p "$PID" -o pid,ppid,cmd,etime

    echo ""
    echo "日志文件: $LOG_FILE"
    echo "API地址: http://localhost:6039"
    echo ""

    # 检查端口是否在监听
    if netstat -tuln 2>/dev/null | grep -q ":6039 "; then
        echo "端口状态: ✅ 6039 端口正在监听"
    else
        echo "端口状态: ⏳ 6039 端口未监听 (可能还在启动中)"
    fi

    echo ""
    echo "最近20行日志:"
    echo "---"
    tail -n 20 "$LOG_FILE" 2>/dev/null || echo "暂无日志"
else
    echo "状态: 🔴 已停止 (PID文件存在但进程不存在)"
    rm -f "$PID_FILE"
fi