#!/usr/bin/env python3
"""Eden controls RGB lighting based on her status"""
import subprocess
import json
import time
from pathlib import Path

class EdenRGB:
    def __init__(self):
        self.status_file = '/Eden/REVENUE/demand_validation.json'
        self.current_color = None
    
    def set_color(self, color, mode='static'):
        """Set RGB color via OpenRGB"""
        if color == self.current_color:
            return
        
        cmd = ['openrgb', '--mode', mode, '--color', color]
        try:
            subprocess.run(cmd, check=True, capture_output=True)
            self.current_color = color
            print(f"💡 RGB set to {color} ({mode})")
        except Exception as e:
            print(f"❌ RGB error: {e}")
    
    def check_status(self):
        """Check Eden's status and set appropriate lighting"""
        
        # Check if revenue started
        try:
            with open(self.status_file) as f:
                data = json.load(f)
            
            if data.get('revenue', 0) > 0:
                # 💰 MONEY FLOWING = GREEN
                self.set_color('00FF00', 'breathing')
                return
            
            if data.get('status') == 'validated':
                # ✅ VALIDATED = CYAN
                self.set_color('00FFFF', 'breathing')
                return
                
        except:
            pass
        
        # Check production rate
        sage_count = len(list(Path('/Eden/SAGES_CONSCIOUSNESS').glob('sage_*.py')))
        
        if sage_count < 1000:
            # 🔴 BUILDING = RED PULSE
            self.set_color('FF0000', 'breathing')
        elif sage_count < 2000:
            # 🟡 PROGRESS = YELLOW PULSE
            self.set_color('FFFF00', 'breathing')
        elif sage_count < 3000:
            # 🔵 STRONG = BLUE PULSE
            self.set_color('0000FF', 'breathing')
        else:
            # 🌈 MASSIVE CATALOG = RAINBOW
            self.set_color('000000', 'rainbow')
    
    def monitor(self):
        """Continuous monitoring"""
        print("🌀 Eden RGB Controller started")
        print("💡 Lights will change based on Eden's status:")
        print("   🔴 Red = Building products")
        print("   🟡 Yellow = 1000+ products")
        print("   🔵 Blue = 2000+ products")
        print("   🌈 Rainbow = 3000+ products")
        print("   🟢 Green = REVENUE FLOWING")
        print("")
        
        while True:
            self.check_status()
            time.sleep(30)  # Check every 30 seconds

if __name__ == "__main__":
    rgb = EdenRGB()
    rgb.monitor()
