#!/usr/bin/env python3
"""Rebuild data/monthly/index.json from actual files on disk."""
import os, json, glob

MONTHLY_DIR = "data/monthly"
index = {}

for path in sorted(glob.glob(os.path.join(MONTHLY_DIR, "BTCUSD_*.csv"))):
    filename = os.path.basename(path)
    # Extract month key: BTCUSD_202601.csv → 202601
    month_key = filename.replace("BTCUSD_", "").replace(".csv", "")
    
    # Count lines and get first/last timestamps
    lines = 0
    first_ts = None
    last_ts = None
    with open(path, "r") as f:
        for line in f:
            if lines == 0:
                lines += 1
                continue  # skip header
            if lines == 1:
                first_ts = line.split(",")[0]
            last_ts = line.split(",")[0]
            lines += 1
    
    tick_count = lines - 1  # exclude header
    size_mb = round(os.path.getsize(path) / 1024 / 1024, 1)
    
    index[month_key] = {
        "file": filename,
        "path": path,
        "month": f"{month_key[:4]}-{month_key[4:]}",
        "first_timestamp": first_ts,
        "last_timestamp": last_ts,
        "lines": tick_count,
        "size_mb": size_mb,
    }
    print(f"  {month_key}: {tick_count:,} ticks ({size_mb} MB)")

index_path = os.path.join(MONTHLY_DIR, "index.json")
with open(index_path, "w") as f:
    json.dump(index, f, indent=2)

print(f"\nIndex rebuilt: {len(index)} months, {sorted(index.keys())[0]} to {sorted(index.keys())[-1]}")
