#!/bin/bash

# Gitea Webhook Ambassador Python 启动脚本

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVICE_NAME="gitea-webhook-ambassador-python"
LOG_FILE="$SCRIPT_DIR/logs/service.log"
PID_FILE="$SCRIPT_DIR/service.pid"

# 创建日志目录
mkdir -p "$SCRIPT_DIR/logs"

# 激活虚拟环境
source "$SCRIPT_DIR/venv/bin/activate"

# 函数：启动服务
start_service() {
    echo "🚀 启动 $SERVICE_NAME..."
    
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p $PID > /dev/null 2>&1; then
            echo "❌ 服务已在运行 (PID: $PID)"
            return 1
        else
            echo "⚠️  发现过期的 PID 文件，正在清理..."
            rm -f "$PID_FILE"
        fi
    fi
    
    # 后台启动服务
    nohup python -m uvicorn app.main_demo:app --host 0.0.0.0 --port 8000 > "$LOG_FILE" 2>&1 &
    PID=$!
    echo $PID > "$PID_FILE"
    
    # 等待服务启动
    sleep 3
    
    if ps -p $PID > /dev/null 2>&1; then
        echo "✅ 服务启动成功 (PID: $PID)"
        echo "📝 日志文件: $LOG_FILE"
        echo "🌐 访问地址: http://localhost:8000"
        echo "🔑 演示密钥: demo_admin_key, demo_user_key"
    else
        echo "❌ 服务启动失败"
        rm -f "$PID_FILE"
        return 1
    fi
}

# 函数：停止服务
stop_service() {
    echo "🛑 停止 $SERVICE_NAME..."
    
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p $PID > /dev/null 2>&1; then
            kill $PID
            echo "✅ 服务已停止 (PID: $PID)"
        else
            echo "⚠️  服务未运行"
        fi
        rm -f "$PID_FILE"
    else
        echo "⚠️  PID 文件不存在"
    fi
}

# 函数：重启服务
restart_service() {
    echo "🔄 重启 $SERVICE_NAME..."
    stop_service
    sleep 2
    start_service
}

# 函数：查看状态
status_service() {
    if [ -f "$PID_FILE" ]; then
        PID=$(cat "$PID_FILE")
        if ps -p $PID > /dev/null 2>&1; then
            echo "✅ $SERVICE_NAME 正在运行 (PID: $PID)"
            echo "📝 日志文件: $LOG_FILE"
            echo "🌐 访问地址: http://localhost:8000"
        else
            echo "❌ $SERVICE_NAME 未运行 (PID 文件存在但进程不存在)"
            rm -f "$PID_FILE"
        fi
    else
        echo "❌ $SERVICE_NAME 未运行"
    fi
}

# 函数：查看日志
show_logs() {
    if [ -f "$LOG_FILE" ]; then
        echo "📝 显示最新日志 (最后 50 行):"
        echo "----------------------------------------"
        tail -n 50 "$LOG_FILE"
        echo "----------------------------------------"
        echo "完整日志文件: $LOG_FILE"
    else
        echo "❌ 日志文件不存在"
    fi
}

# 函数：实时日志
follow_logs() {
    if [ -f "$LOG_FILE" ]; then
        echo "📝 实时日志 (按 Ctrl+C 退出):"
        tail -f "$LOG_FILE"
    else
        echo "❌ 日志文件不存在"
    fi
}

# 主逻辑
case "$1" in
    start)
        start_service
        ;;
    stop)
        stop_service
        ;;
    restart)
        restart_service
        ;;
    status)
        status_service
        ;;
    logs)
        show_logs
        ;;
    follow)
        follow_logs
        ;;
    *)
        echo "用法: $0 {start|stop|restart|status|logs|follow}"
        echo ""
        echo "命令说明:"
        echo "  start   - 启动服务"
        echo "  stop    - 停止服务"
        echo "  restart - 重启服务"
        echo "  status  - 查看服务状态"
        echo "  logs    - 查看最新日志"
        echo "  follow  - 实时查看日志"
        echo ""
        echo "示例:"
        echo "  $0 start    # 启动服务"
        echo "  $0 status   # 查看状态"
        echo "  $0 logs     # 查看日志"
        exit 1
        ;;
esac 