import json
import time

STATE_FILE = "backtest_state.json"
JOURNAL_FILE = "data/journal.json"
SHADOW_FILE = "data/shadow-trades.json"
LOCK_FILE = "backtest.lock"

def update():
    # Update state
    try:
        with open(STATE_FILE, 'r') as f:
            state = json.load(f)
            
        state["next_cursor_date"] = "2026-01-07 14:00:00"
        state["completed_through"] = "2026-01-07 03:30:00 UTC (11:30 WITA approx)"
        state["progress_notes"].append("Batch completed: Advanced M15 replay. Price created a bearish FVG at 4482.36 - 4479.115 and tapped into it around 11:15 UTC. Shadow observation of bearish continuation from this FVG.")
        
        with open(STATE_FILE, 'w') as f:
            json.dump(state, f, indent=2)
            
    except Exception as e:
        print(f"Error updating state: {e}")

    # Add shadow trade
    try:
        with open(SHADOW_FILE, 'r') as f:
            shadows = json.load(f)
            
        new_shadow = {
            "id": f"shadow_{int(time.time())}",
            "timestamp": "2026-01-07 11:15:00 UTC",
            "symbol": "XAUUSD",
            "direction": "Short",
            "strategy": "Bearish FVG Retracement",
            "entry": 4479.115,
            "stop_loss": 4492.415,
            "take_profit": 4460.00,
            "outcome": "pending",
            "pnl_r": 0,
            "notes": "Price displaced down leaving an FVG (4482.36-4479.115). Tapped 4480.52 and rejected. Shadow entry at FVG bottom. Target is recent lows."
        }
        
        shadows.append(new_shadow)
        
        with open(SHADOW_FILE, 'w') as f:
            json.dump(shadows, f, indent=2)
            
    except Exception as e:
        print(f"Error updating shadows: {e}")
        
    # Remove lock
    import os
    if os.path.exists(LOCK_FILE):
        os.remove(LOCK_FILE)
        
if __name__ == "__main__":
    update()
    print("Files updated successfully and lock removed.")
