package discovery

import "strings"

// Rail identifies a canonical payment rail token.
type Rail string

// RailOperation identifies a canonical payment-plan rail action token.
type RailOperation string

const (
	OperationDiscoveryLookup = "discovery.lookup"

	OperationDocumentsBatchResolve = "documents.batch_resolve"
	OperationDocumentsGet          = "documents.get"
	OperationFeeCalc               = "fee.calc"
	OperationNotifySend            = "notify.send"
	OperationFXQuote               = "fx.quote"
	OperationFXIngest              = "fx.ingest"

	OperationPaymentInitiate      = "payment.initiate"
	OperationPaymentQuote         = "payment.quote"
	OperationPaymentMultiQuote    = "payment.multiquote"
	OperationPaymentMethodsManage = "payment_methods.manage"
	OperationPaymentMethodsRead   = "payment_methods.read"

	OperationBalanceRead = "balance.read"

	OperationLedgerDebit    = "ledger.debit"
	OperationLedgerCredit   = "ledger.credit"
	OperationLedgerTransfer = "ledger.transfer"
	OperationLedgerFX       = "ledger.fx"
	OperationExternalDebit  = "external.debit"
	OperationExternalCredit = "external.credit"

	OperationSend           = "send"
	OperationFee            = "fee"
	OperationObserveConfirm = "observe.confirm"
	OperationFXConvert      = "fx.convert"
)

// Canonical rail identifiers.
const (
	RailUnspecified        = "UNSPECIFIED"
	RailCrypto             = "CRYPTO"
	RailProviderSettlement = "SETTLEMENT"
	RailLedger             = "LEDGER"
	RailCardPayout         = "CARD"
	RailFiatOnRamp         = "ONRAMP"
	RailFiatOffRamp        = "OFFRAMP"
)

// Canonical payment-plan rail operation identifiers.
const (
	RailOperationUnspecified    = "UNSPECIFIED"
	RailOperationDebit          = "DEBIT"
	RailOperationCredit         = "CREDIT"
	RailOperationExternalDebit  = "EXTERNAL_DEBIT"
	RailOperationExternalCredit = "EXTERNAL_CREDIT"
	RailOperationMove           = "MOVE"
	RailOperationSend           = "SEND"
	RailOperationFee            = "FEE"
	RailOperationObserveConfirm = "OBSERVE_CONFIRM"
	RailOperationFXConvert      = "FX_CONVERT"
	RailOperationBlock          = "BLOCK"
	RailOperationRelease        = "RELEASE"
)

// NormalizeOperation canonicalizes an operation string for comparisons.
func NormalizeOperation(value string) string {
	return strings.ToLower(strings.TrimSpace(value))
}

// HasAnyOperation reports whether ops contains any of required operations.
func HasAnyOperation(ops []string, required []string) bool {
	if len(required) == 0 {
		return true
	}
	for _, op := range ops {
		normalized := NormalizeOperation(op)
		if normalized == "" {
			continue
		}
		for _, target := range required {
			if normalized == NormalizeOperation(target) {
				return true
			}
		}
	}
	return false
}

// LedgerServiceOperations returns canonical operations announced by ledger.
func LedgerServiceOperations() []string {
	return []string{
		OperationBalanceRead,
		OperationLedgerDebit,
		OperationLedgerCredit,
		OperationLedgerTransfer,
		OperationLedgerFX,
		OperationExternalCredit,
		OperationExternalDebit,
	}
}
