# File: /Users/bpa/Desktop/ForesixaQuant/view_metrics.py
import sqlite3
import time
import os

DB_PATH = "/Users/bpa/Desktop/ForesixaQuant/trading_live.db"

def watch_live_ledger_metrics():
    print("\n============================================================")
    print("     QZOTIC SOVEREIGN LEDGER: LIVE METRICS MONITOR          ")
    print("============================================================")
    print(" Status: Scanning local SQLite core database tables...")
    print(" (Press Ctrl+C to close this dashboard at any time)\n")
    
    last_printed_id = 0
    
    while True:
        try:
            if os.path.exists(DB_PATH):
                conn = sqlite3.connect(DB_PATH)
                cursor = conn.cursor()
                
                # Fetch only new rows that have accumulated since our last print cycle
                cursor.execute("""
                    SELECT id, timestamp, client_name, amount, status, transaction_id 
                    FROM customer_payments_ledger 
                    WHERE id > ? 
                    ORDER BY id ASC
                """, (last_printed_id,))
                
                rows = cursor.fetchall()
                for row in rows:
                    print(f" [TRANSACTION RECORDED] ")
                    print(f"   • Order Log ID   : {row[0]}")
                    print(f"   • Timestamp      : {row[1]}")
                    print(f"   • Client Account : {row[2]}")
                    print(f"   • Gross Amount   : ${row[3]:.2f}")
                    print(f"   • Gateway Status : {row[4]}")
                    print(f"   • Core Unique ID : {row[5]}")
                    print("-" * 45)
                    last_printed_id = row[0]
                    
                conn.close()
        except Exception as e:
            print(f" [DATABASE SLEEP] Waiting for initial row write... ({str(e)})")
            
        time.sleep(2)

if __name__ == "__main__":
    watch_live_ledger_metrics()
