#!/bin/bash
# RQ Worker Startup Script
#
# This script starts RQ workers for processing background jobs.
# Workers listen on configured queues in priority order.
#
# Usage:
#   ./scripts/start_workers.sh           # Start all-queue worker
#   ./scripts/start_workers.sh ai        # Start AI-only worker
#   ./scripts/start_workers.sh combat    # Start combat-only worker
#   ./scripts/start_workers.sh marketplace  # Start marketplace-only worker
#
# Environment Variables:
#   REDIS_URL - Redis connection URL (default: redis://localhost:6379/0)
#   LOG_LEVEL - Logging level (default: INFO)
#   WORKER_COUNT - Number of workers to start (default: 1)

set -e

# Change to API directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
API_DIR="$(dirname "$SCRIPT_DIR")"
cd "$API_DIR"

# Load environment variables if .env exists
if [ -f .env ]; then
    export $(grep -v '^#' .env | xargs)
fi

# Default configuration
REDIS_URL="${REDIS_URL:-redis://localhost:6379/0}"
LOG_LEVEL="${LOG_LEVEL:-INFO}"
WORKER_COUNT="${WORKER_COUNT:-1}"

# Determine which queues to listen on
WORKER_TYPE="${1:-all}"

case "$WORKER_TYPE" in
    ai)
        QUEUES="ai_tasks"
        WORKER_NAME="ai-worker"
        ;;
    combat)
        QUEUES="combat_tasks"
        WORKER_NAME="combat-worker"
        ;;
    marketplace)
        QUEUES="marketplace_tasks"
        WORKER_NAME="marketplace-worker"
        ;;
    all|*)
        QUEUES="ai_tasks,combat_tasks,marketplace_tasks"
        WORKER_NAME="all-queues-worker"
        ;;
esac

echo "=========================================="
echo "Starting RQ Worker"
echo "=========================================="
echo "Worker Type: $WORKER_TYPE"
echo "Worker Name: $WORKER_NAME"
echo "Queues: $QUEUES"
echo "Redis URL: ${REDIS_URL//:*@/:***@}"
echo "Log Level: $LOG_LEVEL"
echo "Worker Count: $WORKER_COUNT"
echo "=========================================="

# Activate virtual environment if it exists
if [ -d "venv" ]; then
    echo "Activating virtual environment..."
    source venv/bin/activate
fi

# Start workers
if [ "$WORKER_COUNT" -eq 1 ]; then
    # Single worker
    echo "Starting single worker..."
    exec rq worker \
        --url "$REDIS_URL" \
        --name "$WORKER_NAME" \
        --logging_level "$LOG_LEVEL" \
        --with-scheduler \
        $QUEUES
else
    # Multiple workers (use supervisord or run in background)
    echo "Starting $WORKER_COUNT workers..."
    for i in $(seq 1 $WORKER_COUNT); do
        rq worker \
            --url "$REDIS_URL" \
            --name "${WORKER_NAME}-${i}" \
            --logging_level "$LOG_LEVEL" \
            $QUEUES &
        echo "Started worker ${WORKER_NAME}-${i} (PID: $!)"
    done

    echo "All workers started. Press Ctrl+C to stop."
    wait
fi
