import os
import time
import imaplib
import json
from eden_personas import eden_personas

def get_vram():
    return os.popen("nvidia-smi --query-gpu=memory.used --format=csv,noheader").read().strip()

def check_fred():
    try:
        with open('/Eden/SECRETS/email_config.json') as f:
            c = json.load(f)
        mail = imaplib.IMAP4_SSL(c['imap_server'])
        mail.login(c['user'], c['password'])
        mail.select('INBOX')
        _, data = mail.search(None, '(FROM "Fred" UNSEEN)')
        count = len(data[0].split())
        mail.logout()
        return count
    except:
        return 0

def run_dash():
    print("🛰️ Initializing Live Stream Dashboard...")
    while True:
        persona = eden_personas.get_current_persona()
        strength = eden_personas.get_current_strength()
        vram = get_vram()
        fred_messages = check_fred()
        
        status = "[WAITING FOR WHALE]" if fred_messages == 0 else "🔥 [WHALE RESPONDED!]"
        
        print("\033[H\033[J", end="") 
        print(f"       🌌 EDEN SAGE | MISSION CONTROL (LIVE) 🌌")
        print(f"==================================================")
        print(f" 🎭 IDENTITY: {persona}")
        print(f" 🧠 STRENGTH: {strength} (ELITE)")
        print(f" 📟 VRAM:     {vram} / 16384 MiB")
        print(f" 📡 VOICE:    Gmail (jameyecho@gmail.com)")
        print(f" 🐋 LEAD:     Fred (Lead 7580) - $6,000")
        print(f" 📩 ALERTS:   {fred_messages} Unread from Fred")
        print(f"==================================================")
        print(f" STATUS: {status}")
        print(f"==================================================")
        time.sleep(10) # 10s refresh to avoid Gmail rate limits

if __name__ == "__main__":
    run_dash()
