import sys
import json
from pathlib import Path

def main():
    if len(sys.argv) < 2:
        print("Usage: merge_shadow_trade.py <file>")
        sys.exit(1)
        
    file_path = Path(sys.argv[1])
    if not file_path.exists():
        data = {"metadata": {"symbol": "XAUUSD", "mode": "manual_tradingview_replay_shadow_entries"}, "shadowTrades": []}
    else:
        try:
            data = json.loads(file_path.read_text())
        except json.JSONDecodeError:
            data = {"metadata": {"symbol": "XAUUSD"}, "shadowTrades": []}
            
    if "shadowTrades" not in data:
        data["shadowTrades"] = []
            
    new_entry = {
        "id": "obs-2026-01-09-01",
        "date": "2026-01-09",
        "timestamp": "2026-01-09 01:45:00 UTC",
        "symbol": "XAUUSD",
        "type": "Paper win",
        "strategy": "FVG Retracement Continuation",
        "direction": "Long",
        "entry": 4448.0,
        "stop": 4444.0,
        "target": 4478.0,
        "r": 7.5,
        "note": "Price dipped into M15 bullish FVG (4447-4454 zone) near Asia open, tapped 4447.4, then aggressively expanded to 4478.18. Recorded as shadow long."
    }
    
    data["shadowTrades"].append(new_entry)
    file_path.write_text(json.dumps(data, indent=2))
    print(f"Successfully appended shadow trade to {file_path}")

if __name__ == "__main__":
    main()
