package com.clanstop.healing.driver;

/**
 * Process-wide run id plus a per-thread "current step" id, shared between the Cucumber
 * event plugin (which owns step rows) and the {@link com.clanstop.healing.locator.LocatorResolver}
 * (which needs the step id to FK-link a heal). A ThreadLocal keeps parallel scenarios isolated.
 */
public final class HealingContext {

    private static volatile long runId = -1;
    private static final ThreadLocal<Long> currentStepId = ThreadLocal.withInitial(() -> -1L);
    private static final ThreadLocal<org.openqa.selenium.WebDriver> currentDriver = new ThreadLocal<>();

    private HealingContext() {}

    public static void setRunId(long id) { runId = id; }
    public static long runId()           { return runId; }

    public static void setCurrentStepId(long id) { currentStepId.set(id); }
    public static long currentStepId()           { return currentStepId.get(); }
    public static void clearCurrentStep()        { currentStepId.remove(); }

    /** The active driver for the current thread, so the logging plugin can screenshot on failure. */
    public static void setCurrentDriver(org.openqa.selenium.WebDriver d) { currentDriver.set(d); }
    public static org.openqa.selenium.WebDriver currentDriver()          { return currentDriver.get(); }
    public static void clearCurrentDriver()                              { currentDriver.remove(); }
}
