"""
Full backfill (read-only): apply the validated Polymarket->live_data timestamp backfill
to EVERY live_data/*.csv, writing a *_polybackfill.csv copy into poly_backfill/. Originals
are never touched. Reuses poly_backfill_pilot.backfill_file (same matcher/fetch verified at
74/74 fixtures, <1c alignment on the 2026-04-25 pilot). NOTHING is traded.
"""
import os, glob, sys, time
sys.path.insert(0, r"E:\soccer\main")
import poly_backfill_pilot as B

sys.stdout.reconfigure(encoding='utf-8', errors='replace')

LIVE_DIR = r"E:\soccer\live_data"
OUT_DIR  = r"E:\soccer\poly_backfill"


def main():
    os.makedirs(OUT_DIR, exist_ok=True)
    files = sorted(glob.glob(os.path.join(LIVE_DIR, "*.csv")))
    print(f"backfilling {len(files)} live_data days -> {OUT_DIR}\n")
    tot = {'rows': 0, 'fixtures': 0, 'matched': 0, 'no_event': 0, 'no_hist': 0, 'filled': 0}
    for f in files:
        day = os.path.splitext(os.path.basename(f))[0]
        out = os.path.join(OUT_DIR, f"{day}_polybackfill.csv")
        t0 = time.time()
        s = B.backfill_file(f, out)
        for k in tot:
            tot[k] += s[k]
        print(f"  {day}: {s['matched']}/{s['fixtures']} fixtures matched, "
              f"{s['filled']} rows filled (no_event {s['no_event']}, no_hist {s['no_hist']}) "
              f"[{time.time()-t0:.0f}s]")
    print(f"\nTOTAL across {len(files)} days: "
          f"{tot['matched']}/{tot['fixtures']} fixtures matched, "
          f"{tot['filled']}/{tot['rows']} rows filled "
          f"(no_event {tot['no_event']}, no_hist {tot['no_hist']})")


if __name__ == '__main__':
    main()
