import json
import datetime
import os

results = []

# Candidate 1: 2026-04-07T15:30:00Z Long
# HTF: H4 huge dump down to 4607, closing near 4657. H1 dump and recovery. 
# M15 sweep at 14:00 down to 4607, M15 structure break above 4663 (13:30 high) or 4654 (13:45 high).
# 15:30 candle is the M15 confirmation/retrace. It swept M15 low to 4640 and closed 4645.
# At 16:00 price spiked to 4668, confirming the long but TP1 is close (4663) and price dumped again at 17:00.
# We will reject it due to bad RR / choppy PA / needs TradingView context.
results.append({
    "candidate_id": "candidate_2026-04-07T15-30-00Z_long_liquidity_sweep_reversal",
    "timestamp": "2026-04-07T15:30:00Z",
    "decision": "needs_more_context",
    "reason": "Massive H4 liquidation down to 4607. M15/M5 PA is highly volatile and choppy. Needs TV replay to verify actual entry zones and Fib alignments.",
    "htf_context": "H4 extreme dump and rejection (4691 to 4607). Massive volatility.",
    "m30_structure": "Deep sweep below 4635, recovery to 4661, then choppy retrace.",
    "m15_m5_execution": "M15 swept 4624 at 15:00 and reclaimed, but price action is erratic with huge wicks.",
    "entry": None,
    "stop": None,
    "target": None,
    "r": None,
    "promoted_to_journal": False
})

# Candidate 2: 2026-04-08T03:15:00Z Long
# Candidate 3: 2026-04-08T03:30:00Z Long
# HTF: Price exploded upwards. H4 at 21:00 on April 7 hit 4858. 
# Currently in an extreme uptrend, retracing to 4788 on H1.
# Both candidates point to M15 sweep at 03:00 / 03:15 (low around 4796-4798). 
# However, control note says "mechanical_sl_first" - this means after confirmation, it hit SL before TP.
# Without TV replay, we can't definitively verify the invalidation / stop hunt. Best to mark needs_more_context.
results.append({
    "candidate_id": "candidate_2026-04-08T03-15-00Z_long_liquidity_sweep_reversal",
    "timestamp": "2026-04-08T03:15:00Z",
    "decision": "needs_more_context",
    "reason": "Control indicates mechanical SL first in a massive uptrend retrace. Needs TV replay to confirm Fib entry and SL placement accuracy.",
    "htf_context": "Extreme H4/H1 uptrend with price jumping from 4650 to 4858 in hours.",
    "m30_structure": "Retracing from 4858 highs. M30 swept 4788 previously and is forming a higher low base around 4795.",
    "m15_m5_execution": "M15 swept 4798 at 03:00, but control logic hit SL first. Needs visual check.",
    "entry": None,
    "stop": None,
    "target": None,
    "r": None,
    "promoted_to_journal": False
})

results.append({
    "candidate_id": "candidate_2026-04-08T03-30-00Z_long_liquidity_sweep_reversal",
    "timestamp": "2026-04-08T03:30:00Z",
    "decision": "needs_more_context",
    "reason": "Same setup as 03:15 candidate. Control indicates mechanical SL first. Needs TV replay.",
    "htf_context": "Extreme H4/H1 uptrend with price jumping from 4650 to 4858 in hours.",
    "m30_structure": "Retracing from 4858 highs. M30 forming a higher low base around 4795.",
    "m15_m5_execution": "M15 swept 4798 at 03:00, but control logic hit SL first. Needs visual check.",
    "entry": None,
    "stop": None,
    "target": None,
    "r": None,
    "promoted_to_journal": False
})

out_dir = "/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/reviews/manual_replay_results"
os.makedirs(out_dir, exist_ok=True)
out_file = f"{out_dir}/replay_result_00469_00472.json"
with open(out_file, "w") as f:
    json.dump(results, f, indent=2)

# Update cursor
progress_file = "/home/aryy/.hermes/profiles/finance/backtests/xauusd_2026/hybrid/reviews/manual_replay_progress.json"
with open(progress_file, "r") as f:
    prog = json.load(f)

prog["cursor"]["processed_count"] += 3
prog["cursor"]["next_index"] = 472
prog["cursor"]["needs_more_context_count"] += 3
prog["cursor"]["last_candidate_id"] = "candidate_2026-04-08T03-30-00Z_long_liquidity_sweep_reversal"
prog["cursor"]["last_updated"] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")

prog["runs"][0]["result_files"].append("hybrid/reviews/manual_replay_results/replay_result_00469_00472.json")
prog["runs"][0]["decision_counts"]["needs_more_context"] += 3

with open(progress_file, "w") as f:
    json.dump(prog, f, indent=2)

print("SUCCESS")
