#!/bin/bash
# Creates all systemd services and timers for shadow bot + LLM system

SHADOW="/opt/services/bots/shadow_bot/Stock_Bot"
LLM="/opt/services/shadow_llm"
VENV="/opt/services/bots/trading_bot/venv/bin/python3"

echo "=== CREATING SHADOW BOT SERVICE ==="
sudo tee /etc/systemd/system/shadow-bot.service > /dev/null << 'EOF'
[Unit]
Description=Shadow Stock Trading Bot (LLM test environment)
After=network-online.target trading-bot.service
Wants=network-online.target

[Service]
Type=simple
User=backule
WorkingDirectory=/opt/services/bots/shadow_bot/Stock_Bot
ExecStart=/opt/services/bots/trading_bot/venv/bin/python3 StockTrading.py --mode both --max-workers 8
Restart=on-failure
RestartSec=60
StandardOutput=append:/opt/services/bots/shadow_bot/Stock_Bot/trading.log
StandardError=append:/opt/services/bots/shadow_bot/Stock_Bot/trading.log
EnvironmentFile=/opt/services/bots/trading_bot/.env
Environment=HOME=/home/backule
Environment=PYTHONUNBUFFERED=1

[Install]
WantedBy=multi-user.target
EOF

echo "=== CREATING LLM RESEARCHER TIMER (market hours) ==="
sudo tee /etc/systemd/system/llm-researcher.service > /dev/null << 'EOF'
[Unit]
Description=Shadow LLM Researcher — runs during market hours
After=network-online.target shadow-bot.service

[Service]
Type=simple
User=backule
WorkingDirectory=/opt/services/shadow_llm
ExecStart=/opt/services/bots/trading_bot/venv/bin/python3 /opt/services/shadow_llm/researcher.py
Restart=on-failure
RestartSec=300
StandardOutput=append:/opt/services/shadow_llm/researcher.log
StandardError=append:/opt/services/shadow_llm/researcher.log
Environment=HOME=/home/backule

[Install]
WantedBy=multi-user.target
EOF

echo "=== CREATING LLM IMPROVER TIMER (after EOD train) ==="
sudo tee /etc/systemd/system/llm-improver.timer > /dev/null << 'EOF'
[Unit]
Description=Shadow LLM Improver — runs at 6pm ET daily

[Timer]
OnCalendar=*-*-* 23:00:00 UTC
Persistent=true

[Install]
WantedBy=timers.target
EOF

sudo tee /etc/systemd/system/llm-improver.service > /dev/null << 'EOF'
[Unit]
Description=Shadow LLM Improver — generates improvement proposals

[Service]
Type=oneshot
User=backule
WorkingDirectory=/opt/services/shadow_llm
ExecStart=/opt/services/bots/trading_bot/venv/bin/python3 /opt/services/shadow_llm/improver.py
StandardOutput=append:/opt/services/shadow_llm/improver.log
StandardError=append:/opt/services/shadow_llm/improver.log
Environment=HOME=/home/backule
EOF

echo "=== CREATING LLM EVALUATOR TIMER (8am ET = 1pm UTC) ==="
sudo tee /etc/systemd/system/llm-evaluator.timer > /dev/null << 'EOF'
[Unit]
Description=Shadow LLM Evaluator — daily comparison report

[Timer]
OnCalendar=*-*-* 13:00:00 UTC
Persistent=true

[Install]
WantedBy=timers.target
EOF

sudo tee /etc/systemd/system/llm-evaluator.service > /dev/null << 'EOF'
[Unit]
Description=Shadow LLM Evaluator — shadow vs real comparison

[Service]
Type=oneshot
User=backule
WorkingDirectory=/opt/services/shadow_llm
ExecStart=/opt/services/bots/trading_bot/venv/bin/python3 /opt/services/shadow_llm/evaluator.py
StandardOutput=append:/opt/services/shadow_llm/evaluator.log
StandardError=append:/opt/services/shadow_llm/evaluator.log
Environment=HOME=/home/backule
EOF

echo "=== RELOADING SYSTEMD ==="
sudo systemctl daemon-reload
sudo systemctl enable shadow-bot
sudo systemctl enable llm-improver.timer
sudo systemctl enable llm-evaluator.timer
echo "Services created and enabled"
echo "Note: llm-researcher started manually when you want research to run"
