package records

import (
	"os"
	"strconv"
	"strings"
)

const (
	fieldHistorySequenceReservationEnabledEnv = "NEXUSCRM_FIELD_HISTORY_SEQUENCE_RESERVATION_ENABLED"
)

// featureDisabledFromEnv reads a fail-safe "…_ENABLED" circuit breaker: the
// feature stays ON when the setting is absent, empty, or unparseable, and only
// a value that parses explicitly as false turns it off. An operator who
// mistypes the flag gets the safe behaviour, not silence.
//
// Shared by every such switch in this package so the semantics are defined
// once rather than re-derived per feature.
func featureDisabledFromEnv(name string) bool {
	raw := strings.TrimSpace(os.Getenv(name))
	if raw == "" {
		return false
	}
	enabled, err := strconv.ParseBool(raw)
	return err == nil && !enabled
}

// fieldHistorySequenceReservationDisabledFromEnv is the emergency circuit
// breaker for the per-FieldId sequence rows. Generic Audit V2 remains active;
// update diffs emitted while the breaker is active carry no HistorySequence
// and are therefore hidden by the forward-only Field History query contract.
func fieldHistorySequenceReservationDisabledFromEnv() bool {
	return featureDisabledFromEnv(fieldHistorySequenceReservationEnabledEnv)
}
