package example.obrh;

import java.time.Duration;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;

/** Resilience4j 1.7.1 API illustration, not an existing deployed OBRH breaker. */
public final class ProviderBreakerExample {
    private ProviderBreakerExample() { }

    public static CircuitBreaker create() {
        CircuitBreakerConfig config = CircuitBreakerConfig.custom()
                .slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
                .slidingWindowSize(20)
                .minimumNumberOfCalls(10)
                .failureRateThreshold(50)
                .waitDurationInOpenState(Duration.ofSeconds(30))
                .permittedNumberOfCallsInHalfOpenState(2)
                // Other exceptions (e.g. local overload / pre-send lease rejection)
                // must neither increment provider failures nor count as successes.
                .recordException(error -> error instanceof ProviderFailure)
                .ignoreException(error -> !(error instanceof ProviderFailure))
                .build();
        return CircuitBreaker.of("example-provider-operation", config);
    }

    /** Connector maps a confirmed provider failure to this type; preserve its cause. */
    public static final class ProviderFailure extends RuntimeException {
        private static final long serialVersionUID = 1L;
        public ProviderFailure(String message, Throwable cause) {
            super(message, cause);
        }
    }
    // Execute synchronously with breaker.executeCallable(() -> bulkheads.invoke(...)).
    // Inspect actual exception wrapping before applying the classification predicate.
    // This does not permit unsafe retry, reserve threads, or clear operator route STOP.
    // HALF_OPEN work still needs endpoint admission, finite timeouts and safe semantics.
}
