package xplan

import "strings"

// GuardOperationName identifies non-rail guard operation codes.
type GuardOperationName string

const (
	GuardOperationUnspecified         GuardOperationName = ""
	GuardOperationQuoteReadinessGuard GuardOperationName = "quote.readiness.guard"
	GuardOperationPrefundingEnsure    GuardOperationName = "prefunding.ensure"
)

const (
	QuoteReadinessGuardStepRef = "quote_readiness"
	PrefundingGuardStepRef     = "prefunding_ensure"
)

func ParseGuardOperationName(value string) GuardOperationName {
	switch strings.ToLower(strings.TrimSpace(value)) {
	case string(GuardOperationQuoteReadinessGuard):
		return GuardOperationQuoteReadinessGuard
	case string(GuardOperationPrefundingEnsure):
		return GuardOperationPrefundingEnsure
	default:
		return GuardOperationUnspecified
	}
}

func IsLiquidityGuardOperation(value string) bool {
	return ParseGuardOperationName(value) == GuardOperationQuoteReadinessGuard
}

func IsPrefundingGuardOperation(value string) bool {
	return ParseGuardOperationName(value) == GuardOperationPrefundingEnsure
}

func GuardStepKind(step Step) StepKind {
	switch strings.ToLower(strings.TrimSpace(string(step.Kind))) {
	case string(StepKindLiquidityCheck):
		return StepKindLiquidityCheck
	case string(StepKindPrefunding):
		return StepKindPrefunding
	}
	if IsLiquidityGuardOperation(step.StepCode) {
		return StepKindLiquidityCheck
	}
	if IsPrefundingGuardOperation(step.StepCode) {
		return StepKindPrefunding
	}
	return StepKindUnspecified
}
