import json
import os
import sys

chunk_id = "M15_0001"
suffix = "M15"
chunk_from = 1767225600
chunk_to = 1767484800
tolerance = 900
output_file = "hybrid/data/ohlcv_M15.json"
state_file = "hybrid/collector_state.json"

tv_data_str = sys.stdin.read()
try:
    tv_data = json.loads(tv_data_str)
    if isinstance(tv_data, dict) and "result" in tv_data:
        tv_data = json.loads(tv_data["result"])
except Exception as e:
    print(f"Error parsing TV data: {e}")
    sys.exit(1)

# Filter bars within requested range (with tolerance)
valid_bars = [b for b in tv_data["bars"] if chunk_from - tolerance <= b["time"] <= chunk_to + tolerance]

print(f"DEBUG: TV returned {tv_data['bar_count']} bars. Filtered to {len(valid_bars)} bars. first={tv_data['bars'][0]['time']}")

# Update collector state
with open(state_file, "r") as f:
    state = json.load(f)

# Find chunk
chunk_idx = next(i for i, c in enumerate(state["chunks"]) if c["id"] == chunk_id)
chunk = state["chunks"][chunk_idx]

chunk["attempts"] += 1
if len(valid_bars) > 0:
    chunk["status"] = "done"
    chunk["bars_collected"] = len(valid_bars)
    chunk["last_error"] = None
else:
    chunk["status"] = "failed"
    chunk["last_error"] = f"No bars returned in valid range. first TV bar: {tv_data['bars'][0]['time']} vs chunk_from {chunk_from}"

state["chunks"][chunk_idx] = chunk

# Load or init output file
os.makedirs(os.path.dirname(output_file), exist_ok=True)
if os.path.exists(output_file):
    with open(output_file, "r") as f:
        out_data = json.load(f)
else:
    out_data = {
        "metadata": {
            "symbol": "OANDA:XAUUSD",
            "timeframe": suffix,
            "source": "TradingView MCP",
            "start": valid_bars[0]["time"] if valid_bars else None,
            "end": valid_bars[-1]["time"] if valid_bars else None,
            "bar_count": 0,
            "last_updated": None
        },
        "bars": []
    }

# Merge bars
existing_times = {b["time"] for b in out_data["bars"]}
new_bars = [b for b in valid_bars if b["time"] not in existing_times]
out_data["bars"].extend(new_bars)
out_data["bars"].sort(key=lambda x: x["time"])

# Update metadata
if out_data["bars"]:
    out_data["metadata"]["start"] = out_data["bars"][0]["time"]
    out_data["metadata"]["end"] = out_data["bars"][-1]["time"]
out_data["metadata"]["bar_count"] = len(out_data["bars"])
out_data["metadata"]["last_updated"] = chunk["to_iso"] # approximate

# Write output file
with open(output_file, "w") as f:
    json.dump(out_data, f, indent=2)

# Write state file
with open(state_file, "w") as f:
    json.dump(state, f, indent=2)

# Summary
done_chunks = sum(1 for c in state["chunks"] if c["status"] == "done")
total_chunks = len(state["chunks"])
next_chunk = next((c["id"] for c in state["chunks"] if c["status"] in ["pending", "failed"] and c["attempts"] < 3), "None")

print(f"RESULT: chunk_id={chunk_id}, timeframe={suffix}, date_range={chunk_from}-{chunk_to}, bars_added={len(new_bars)}, progress={done_chunks}/{total_chunks}, next_chunk={next_chunk}")
