import json
import os
import sys

def analyze_candidate(ohlcv_path, candidate_timestamp, timeframe, window=10):
    # Very basic evaluation for context using local JSON
    try:
        with open(ohlcv_path, 'r') as f:
            data = json.load(f)
        
        # Simple string matching for timestamp - assumes ISO format
        idx = -1
        for i, bar in enumerate(data):
            if str(bar.get('time', '')) == str(candidate_timestamp) or str(bar.get('timestamp', '')) == str(candidate_timestamp):
                idx = i
                break
        
        if idx == -1:
            return {"status": "not_found", "message": f"Timestamp {candidate_timestamp} not found in {timeframe}"}
        
        start_idx = max(0, idx - window)
        end_idx = min(len(data), idx + window + 1)
        
        context_bars = data[start_idx:end_idx]
        
        high = max(b.get('high', 0) for b in context_bars)
        low = min(b.get('low', float('inf')) for b in context_bars)
        
        return {
            "status": "success", 
            "context": f"Found at index {idx}. Range High: {high}, Low: {low}. Trend indicates potential.",
            "bars": len(context_bars)
        }
    except Exception as e:
        return {"status": "error", "message": str(e)}

def main():
    if len(sys.argv) < 2:
        print("Usage: python evaluate_candidates.py <candidate_timestamp_1> <candidate_timestamp_2> ...")
        sys.exit(1)
        
    timestamps = sys.argv[1:]
    base_dir = "/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/data"
    
    results = {}
    for ts in timestamps:
        results[ts] = {
            "M30": analyze_candidate(f"{base_dir}/ohlcv_M30.json", ts, "M30"),
            "M15": analyze_candidate(f"{base_dir}/ohlcv_M15.json", ts, "M15"),
            "M5": analyze_candidate(f"{base_dir}/ohlcv_M5.json", ts, "M5")
        }
        
    print(json.dumps(results, indent=2))

if __name__ == "__main__":
    main()
