import json
import datetime
import pandas as pd

def load_data():
    files = {
        'H4': 'hybrid/data/ohlcv_H4.json',
        'H1': 'hybrid/data/ohlcv_H1.json',
        'M30': 'hybrid/data/ohlcv_M30.json',
        'M15': 'hybrid/data/ohlcv_M15.json',
        'M5': 'hybrid/data/ohlcv_M5.json'
    }
    
    dfs = {}
    for tf, file_path in files.items():
        with open(file_path, 'r') as f:
            data = json.load(f)
            df = pd.DataFrame(data['bars'])
            df['datetime'] = pd.to_datetime(df['time'], utc=True)
            df.set_index('datetime', inplace=True)
            dfs[tf] = df
    return dfs

dfs = load_data()

candidates = [
    {"id": "candidate_2026-02-23T07-00-00Z_short", "ts": "2026-02-23T07:00:00Z", "dir": "Short"},
    {"id": "candidate_2026-02-23T09-30-00Z_long", "ts": "2026-02-23T09:30:00Z", "dir": "Long"},
    {"id": "candidate_2026-02-23T12-00-00Z_long", "ts": "2026-02-23T12:00:00Z", "dir": "Long"}
]

for c in candidates:
    print(f"\n--- {c['id']} ({c['dir']}) ---")
    ts = pd.to_datetime(c['ts'])
    
    print("H4 recent context:")
    h4_slice = dfs['H4'].loc[:ts].tail(3)
    if not h4_slice.empty:
        print(h4_slice[['open', 'high', 'low', 'close']])
        
    print("\nH1 recent context:")
    h1_slice = dfs['H1'].loc[:ts].tail(3)
    if not h1_slice.empty:
        print(h1_slice[['open', 'high', 'low', 'close']])

    print("\nM30 structure around candidate:")
    m30_slice = dfs['M30'].loc[ts - pd.Timedelta(hours=4): ts + pd.Timedelta(hours=2)]
    if not m30_slice.empty:
        print(m30_slice[['open', 'high', 'low', 'close']])

    print("\nM15 execution around candidate:")
    m15_slice = dfs['M15'].loc[ts - pd.Timedelta(hours=2): ts + pd.Timedelta(hours=2)]
    if not m15_slice.empty:
        print(m15_slice[['open', 'high', 'low', 'close']])
        
    print("\nM5 execution around candidate:")
    m5_slice = dfs['M5'].loc[ts - pd.Timedelta(hours=1): ts + pd.Timedelta(hours=2)]
    if not m5_slice.empty:
        print(m5_slice[['open', 'high', 'low', 'close']])
