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

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

def parse_growth_telemetry():
    print("\n============================================================")
    print("     QZOTIC SOVEREIGN ENGINE: 90-DAY LAUNCH PERFORMANCE     ")
    print("============================================================")
    print(f" Timestamp Log Pass : {time.strftime('%Y-%m-%d %H:%M:%S')}")
    print(" Track State        : Continuous Sandbox/Paper Operational ")
    print("============================================================\n")

    # If the file path is missing, build it natively with the correct tables
    if not os.path.exists(DB_PATH):
        os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)

    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        # Enforce the creation of the table strictly inside the tracker script itself
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS corporate_trade_ledger (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
                symbol TEXT, side TEXT, qty TEXT, price TEXT,
                allocation_spent TEXT, status TEXT, alpaca_order_id TEXT
            )
        """)
        conn.commit()
        
        # Calculate summary metrics
        cursor.execute("SELECT COUNT(*), SUM(CAST(allocation_spent AS REAL)) FROM corporate_trade_ledger")
        total_trades, total_allocated = cursor.fetchone()
        
        total_trades = total_trades or 0
        total_allocated = total_allocated or 0.0

        print(f" 📈 CORE TRANSACTION METRICS:")
        print(f"   • Total Flow State Trades Run : {total_trades}")
        print(f"   • Total Capital Volume Multiplied: ${total_allocated:.2f}")
        print("-" * 60)
        print(" [STABLE] Waiting for initial algorithmic entry signals to break resistance charts...")
        print("\n============================================================\n")
        
        conn.close()

    except Exception as e:
        print(f" [DATABASE ERROR]: {str(e)}")

if __name__ == "__main__":
    parse_growth_telemetry()
