package formula

import "github.com/nexuscrm/kernel/pkg/formula/spec"

// casesafeIDBundleExprBody is the formula_expr_v1 expression that implements
// CASESAFEID's 15→18 character ID conversion using only generic formula
// operators (ISBLANK, LEN, IF, FIND, MID, arithmetic, concatenation). This
// body lives in CRM pack data — no CASESAFEID-specific compiled Go code ships
// in the platform binary.
//
// Concatenation uses & (Salesforce text-concat operator), not +. At runtime,
// compileFormulaExprV1Evaluator applies replaceFormulaOperators which converts
// & to + before the expression engine's patcher rewrites it to __SFPLUS__ for
// correct Salesforce string-concat semantics. In the DSL SQL compiler, &
// lowers to CONCAT(). In the expression SQL walker, & is not valid expr-lang
// syntax and causes fail-closed — preventing silent numeric-addition divergence.
//
// Algorithm: for a 15-char input, split into 3 groups of 5. For each group,
// compute a 5-bit checksum where bit i is set when char[i] is uppercase A-Z
// (detected via FIND in the uppercase alphabet). Map the 5-bit value (0-31)
// to a character in "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345" via MID. Append the
// three checksum characters. 18-char inputs pass through; nil and other
// lengths pass through unchanged.
const casesafeIDBundleExprBody = `IF(ISBLANK(arg0), arg0, IF(LEN(arg0) == 18, arg0, IF(LEN(arg0) == 15, arg0 & MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 1 + (IF(FIND(MID(arg0,1,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,1,0) + IF(FIND(MID(arg0,2,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,2,0) + IF(FIND(MID(arg0,3,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,4,0) + IF(FIND(MID(arg0,4,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,8,0) + IF(FIND(MID(arg0,5,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,16,0)), 1) & MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 1 + (IF(FIND(MID(arg0,6,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,1,0) + IF(FIND(MID(arg0,7,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,2,0) + IF(FIND(MID(arg0,8,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,4,0) + IF(FIND(MID(arg0,9,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,8,0) + IF(FIND(MID(arg0,10,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,16,0)), 1) & MID("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", 1 + (IF(FIND(MID(arg0,11,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,1,0) + IF(FIND(MID(arg0,12,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,2,0) + IF(FIND(MID(arg0,13,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,4,0) + IF(FIND(MID(arg0,14,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,8,0) + IF(FIND(MID(arg0,15,1),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")>0,16,0)), 1), arg0)))`

// casesafeIDTestBundle returns the CASESAFEID function bundle definition used
// by tests to register CASESAFEID from CRM pack data (L1 content) on a test
// engine instance.
func casesafeIDTestBundle() FunctionBundle {
	return FunctionBundle{
		Functions: []FunctionDefinition{{
			Name:    "CASESAFEID",
			MinArgs: 1,
			MaxArgs: 1,
			Return:  FunctionReturnDefinition{Kind: "fixed", Type: spec.TypeString},
			Args:    []spec.ArgSpec{{Type: spec.TypeString, Nullable: true}},
			Evaluator: FunctionEvaluator{
				Kind:   FormulaExprV1EvaluatorKind,
				Params: []string{"arg0"},
				Body:   casesafeIDBundleExprBody,
			},
		}},
	}
}
