#!/bin/bash
# ops/status.sh — Quick status of both bots + watcher
# Usage: bash ops/status.sh

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

echo -e "=== Bot System Status === $(date '+%Y-%m-%d %H:%M:%S')"
echo

# Watcher
if pgrep -f 'watcher_v4.py' > /dev/null; then
  WPID=$(pgrep -f 'watcher_v4.py')
  WRAM=$(ps -p $WPID -o rss= 2>/dev/null | awk '{printf "%.0fMB", $1/1024}')
  echo -e "  Watcher   RUNNING PID $WPID  RAM $WRAM"
else
  echo -e "  Watcher   DOWN"
fi

# Trading bot
if pgrep -f 'trading_bot/Stock_Bot/StockTrading' > /dev/null; then
  TPID=$(pgrep -f 'trading_bot/Stock_Bot/StockTrading')
  TRAM=$(ps -p $TPID -o rss= 2>/dev/null | awk '{printf "%.0fMB", $1/1024}')
  TCPU=$(ps -p $TPID -o %cpu= 2>/dev/null | xargs)
  echo -e "  Trading   RUNNING PID $TPID  RAM $TRAM  CPU ${TCPU}%"
else
  echo -e "  Trading   DOWN"
fi

# Shadow bot
if pgrep -f 'shadow_bot/Stock_Bot/StockTrading' > /dev/null; then
  SPID=$(pgrep -f 'shadow_bot/Stock_Bot/StockTrading')
  SRAM=$(ps -p $SPID -o rss= 2>/dev/null | awk '{printf "%.0fMB", $1/1024}')
  SCPU=$(ps -p $SPID -o %cpu= 2>/dev/null | xargs)
  echo -e "  Shadow    RUNNING PID $SPID  RAM $SRAM  CPU ${SCPU}%"
else
  echo -e "  Shadow    DOWN"
fi

echo
echo -e "--- System ---"
free -m | awk '/Mem:/ {printf "  RAM: %d MB used / %d MB total (%.0f%% free)\n", $3, $2, $4/$2*100}'
echo

echo -e "--- Recent trading_bot log ---"
tail -5 /opt/services/bots/trading_bot/Stock_Bot/trading.log 2>/dev/null | sed 's/^/  /'
echo

echo -e "--- Recent shadow_bot log ---"
tail -5 /opt/services/bots/shadow_bot/Stock_Bot/trading.log 2>/dev/null | sed 's/^/  /'
