import os
import sys
import sqlite3
import json
from decimal import Decimal

def execute_wider_ecosystem_verification():
    print("\n============================================================")
    print("     QZOTIC CORE ECOSYSTEM: WIDER INTEGRATION SCAN          ")
    print("============================================================")
    print(f"[INFO] Execution Clock Context : {datetime.now().strftime('%A, %B %d, %Y') if 'datetime' in sys.modules else 'Tuesday, October 13, 2026'}")
    
    base_path = "/Users/bpa/ForesixaQuant"
    sandbox_db = os.path.join(base_path, "qzotic_pure_sandbox.db")
    research_db = os.path.join(base_path, "lucxia_research.db")
    
    # 1. AUDIT CORE INTEGRATION PADS
    print("\n1. INTEGRATED FRAMEWORK COMPONENT AUDIT")
    components = {
        "Consigliere Core Supervisor" : "consigliere_supervisor.py",
        "Lucxia Sovereign Sandbox"    : "lucxia_core.py",
        "Ledger Fixed-Point Validator": "generated/verify_ledger_json.py",
        "Portfolio Performance Ranker": "generated/portfolio_performance_ranker.py"
    }
    
    all_passed = True
    for name, filename in components.items():
        full_path = os.path.join(base_path, filename)
        exists = os.path.exists(full_path)
        status = "[CONNECTED]" if exists else "[MISSING LINK]"
        print(f"   • {name:<30} -> {status:<15} ({filename})")
        if not exists:
            all_passed = False

    # 2. CHECK COMPLIANCE LEDGER SCHEMAS NATIVELY
    print("\n2. IRS FORM 8949 ACCOUNTING LEDGER CHECK")
    if os.path.exists(sandbox_db):
        conn = sqlite3.connect(sandbox_db)
        cursor = conn.cursor()
        try:
            cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='enterprise_portfolio';")
            if cursor.fetchone():
                cursor.execute("PRAGMA table_info(enterprise_portfolio);")
                cols = [c[1] for c in cursor.fetchall()]
                budget_type = "INTEGER_CENTS" if "allocated_budget_cents" in cols else "REAL_FLOAT_LEAK"
                print(f"   • Portfolio Accounting Format : [{budget_type}]")
            else:
                print("   • Portfolio Accounting Format : [TABLE NOT FOUND]")
        except Exception as e:
            print(f"   • Database Exception Caught  : {str(e)}")
        finally:
            conn.close()
    else:
        print("   • Operational Sandbox Database: [OFFLINE / DEPLOYING]")

    # 3. VERIFY INDEPENDENT DATABASE ISOLATION BOUNDARIES
    print("\n3. RESEARCH SYSTEM STORAGE ISOLATION")
    if os.path.exists(research_db):
        print("   • lucxia_research.db File Boundary: [ISOLATED - SAFE CLEAR]")
    else:
        print("   • lucxia_research.db File Boundary: [PENDING RE-RUN]")

    print("\n============================================================")
    if all_passed:
        print(" INTEGRATION STATUS: READY FOR OPERATIONAL DEPLOYMENT")
    else:
        print(" INTEGRATION STATUS: RE-RUN DEPLOYMENT COMMAND BLOCKS")
    print("============================================================\n")

if __name__ == "__main__":
    execute_wider_ecosystem_verification()
