package com.clanstop.healing.hooks;

import com.clanstop.healing.config.FrameworkConfig;
import com.clanstop.healing.driver.DriverManager;
import io.cucumber.java.After;
import io.cucumber.java.AfterStep;

/**
 * Lifecycle glue. The heavy lifting (browser creation) is lazy inside {@link DriverManager},
 * so this only needs to guarantee the browser is closed after each scenario. Because
 * {@link DriverManager} is picocontainer-scoped, the instance injected here is the very same
 * one injected into the step classes.
 */
public final class DriverHooks {

    private final DriverManager drivers;

    public DriverHooks(DriverManager drivers) {
        this.drivers = drivers;
    }

    /** Slow-mo: pause after every step so a run is easy to watch. Mirrors the recorder's setting. */
    @AfterStep
    public void slowMo() {
        long pause = FrameworkConfig.get().stepPauseMs();
        if (pause > 0) {
            try {
                Thread.sleep(pause);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    @After
    public void tearDown() {
        if (drivers.started()) {
            drivers.quit();
        }
    }
}
