#!/bin/bash
# Fix alternating 2-hour self-improvement schedule

# Remove old cron, rebuild from scratch
crontab -r

# Create new crontab
cat > /tmp/new_crontab << "CRON"
# Fleet monitoring (every 5 min)
*/5 * * * * /opt/dyeverse/agency/fleet_monitor.sh >> /opt/dyeverse/agency/logs/fleet_monitor.log 2>&1
*/5 * * * * /opt/dyeverse/venv/bin/python /opt/dyeverse/agency/generate_snapshot.py >> /opt/dyeverse/agency/logs/snapshot.log 2>&1

# Daily global risk reset (midnight UTC)
0 0 * * * cd /opt/dyeverse/crypto_agency && /opt/dyeverse/crypto_venv/bin/python3 -c "from core.data.global_risk_db import GlobalRiskDB; db = GlobalRiskDB(\"dexbot\"); db.daily_reset()" >> /var/log/global_risk_reset.log 2>&1

# Legacy checks (4x daily)
0 0,6,12,18 * * * /opt/dyeverse/venv/bin/python /opt/dyeverse/agency/delta_check.py >> /opt/dyeverse/agency/logs/delta_check.log 2>&1
0 0,6,12,18 * * * /opt/dyeverse/venv/bin/python /opt/dyeverse/agency/shadow_trader.py >> /opt/dyeverse/agency/logs/shadow_trader.log 2>&1

# Weather settlement + reporting (daily)
0 10 * * * cd /opt/dyeverse/agency && /opt/dyeverse/venv/bin/python agency/auto_settle_weather.py >> /opt/dyeverse/agency/logs/settlement.log 2>&1
0 11 * * * cd /opt/dyeverse/agency && /opt/dyeverse/venv/bin/python agency/daily_pnl_report.py >> /opt/dyeverse/agency/logs/daily_pnl.log 2>&1
0 11 * * * cd /opt/dyeverse/agency && /opt/dyeverse/venv/bin/python agency/nightly_report.py >> /opt/dyeverse/agency/logs/nightly_report.log 2>&1

# Miami graduation check (daily 7 AM UTC)
0 7 * * * /opt/dyeverse/venv/bin/python /opt/dyeverse/agency/miami_graduation.py >> /opt/dyeverse/agency/logs/miami_graduation.log 2>&1

# Data syncs (daily midnight)
15 0 * * * /opt/dyeverse/agency/sync_weather_data.sh >> /opt/dyeverse/agency/logs/sync_weather.log 2>&1
5 0 * * * /opt/dyeverse/crypto_agency/sync_rl_data.sh >> /opt/dyeverse/crypto_agency/logs/sync_rl_data.log 2>&1

# ALTERNATING 2-HOUR SELF-IMPROVEMENT LOOPS
# Crypto worker: ODD hours (1,3,5,7,9,11,13,15,17,19,21,23 UTC)
0 1,3,5,7,9,11,13,15,17,19,21,23 * * * cd /opt/dyeverse/crypto_agency && /opt/dyeverse/crypto_venv/bin/python3 worker_crypto.py >> /opt/dyeverse/crypto_agency/logs/worker_crypto.log 2>&1

# Polymarket worker: EVEN hours (0,2,4,6,8,10,12,14,16,18,20,22 UTC)
0 0,2,4,6,8,10,12,14,16,18,20,22 * * * cd /opt/dyeverse/crypto_agency && /opt/dyeverse/crypto_venv/bin/python3 worker_poly.py >> /opt/dyeverse/crypto_agency/logs/worker_poly.log 2>&1
CRON

# Install new crontab
crontab /tmp/new_crontab
echo "✅ Crontab updated"
crontab -l
