import sys, os
sys.path.insert(0, "/app")
from tools.factor_detail_charts import parse, R_PX, R_F, LOG, FK
import re

print(f"LOG: {LOG}, exists: {os.path.exists(LOG)}, size: {os.path.getsize(LOG)}")

lines = open(LOG).readlines()
print(f"Lines: {len(lines)}")

# Test first few BTC lines
px_count = f_count = 0
for l in lines:
    if '[BTC]' in l and '$' in l:
        m = R_PX.search(l)
        if m:
            px_count += 1
            if px_count == 1:
                print(f"PX match: ts={m.group(1)} price={m.group(2)}")
    if 'TR:' in l and '=>' in l:
        m = R_F.search(l)
        if m:
            f_count += 1
            if f_count == 1:
                print(f"F match: ts={m.group(1)} total={m.group(2)} dir={m.group(3)}")
                # Test factor extraction
                for fk in FK[:3]:
                    fm = re.search(rf'\b{fk}:([+-]?\d+\.?\d*)', l)
                    val = float(fm.group(1)) if fm else None
                    print(f"  {fk}: {val}")

print(f"PX: {px_count}, F: {f_count}")
r, e = parse()
print(f"Parsed: {len(r)} rows, {len(e)} events")
