# engine/ledger.py
import pandas as pd
from pathlib import Path
from datetime import datetime

LEDGER_FILE = Path("storage/ledger.csv")

def log_event(event, payload):
    row = {
        "timestamp": datetime.utcnow().isoformat(),
        "event": event,
        **payload
    }

    df = pd.DataFrame([row])

    LEDGER_FILE.parent.mkdir(exist_ok=True)

    if LEDGER_FILE.exists():
        df.to_csv(LEDGER_FILE, mode="a", header=False, index=False)
    else:
        df.to_csv(LEDGER_FILE, index=False)
