import time
import os
import sys
from datetime import datetime

LEDGER_FILE = "/Eden/DATA/consolidated_revenue_ledger.txt"
DAILY_TARGET = 481392.50

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

def parse_ledger():
    total_profit = 0.0
    entries = []
    
    if not os.path.exists(LEDGER_FILE):
        return 0.0, []

    with open(LEDGER_FILE, 'r') as f:
        lines = f.readlines()
    
    for line in lines:
        if "Profit: C$" in line:
            try:
                parts = line.split('|')
                profit_str = parts[1].split('C$')[1].strip().replace(',', '')
                profit = float(profit_str)
                total_profit += profit
                entries.append(profit)
            except:
                continue
    return total_profit, entries

def draw_progress_bar(current, total, width=50):
    percent = min(1.0, current / total)
    filled_length = int(width * percent)
    bar = '█' * filled_length + '-' * (width - filled_length)
    return f"|{bar}| {percent:.1%}"

def main():
    print("Starting Eden Financial Dashboard...")
    while True:
        clear_screen()
        current_profit, entries = parse_ledger()
        
        print(f"\n╔════════════════════════════════════════════════════════════╗")
        print(f"║   🏰  VICTORIA FINANCIAL TARGET DASHBOARD  (LIVE)      ║")
        print(f"╚════════════════════════════════════════════════════════════╝")
        print(f"DATE: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
        print(f"STATUS: 🟢 ARBITRAGE PROTOCOL ACTIVE\n")

        print(f"💰 DAILY TARGET:   C${DAILY_TARGET:,.2f}")
        print(f"💵 ACTUAL PROFIT:  C${current_profit:,.2f}")
        
        gap = DAILY_TARGET - current_profit
        if gap > 0:
            print(f"📉 REMAINING:      C${gap:,.2f}")
        else:
            print(f"🚀 SURPLUS:        C${abs(gap):,.2f} (TARGET EXCEEDED)")

        print(f"\nPROGRESS:")
        print(draw_progress_bar(current_profit, DAILY_TARGET))
        
        print(f"\n📊 RECENT TRANSACTIONS:")
        print(f"--------------------------------------------------------------")
        if entries:
            for i, amt in enumerate(entries[-5:]): 
                print(f"   Tx #{len(entries)-4+i}: + C${amt:,.2f}")
        else:
            print("   No transactions recorded yet.")
            
        print(f"--------------------------------------------------------------")
        print(f"Press Ctrl+C to exit monitoring.")
        
        time.sleep(5)

if __name__ == "__main__":
    main()
