package expression

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestFIND_Unicode(t *testing.T) {
	e := NewEngine()

	tests := []struct {
		name     string
		formula  string
		expected interface{}
	}{
		// ASCII
		{"ASCII basic", `FIND("b", "abc")`, 2},
		{"ASCII not found", `FIND("z", "abc")`, 0},
		{"ASCII multi-char", `FIND("cd", "abcdef")`, 3},

		// CJK characters (3 bytes each in UTF-8)
		{"CJK position 1", `FIND("日", "日本語テスト")`, 1},
		{"CJK position 3", `FIND("語", "日本語テスト")`, 3},
		{"CJK position 5", `FIND("ス", "日本語テスト")`, 5},

		// Emoji (4 bytes in UTF-8)
		{"Emoji in string", `FIND("🎉", "hello🎉world")`, 6},
		{"Char after emoji", `FIND("w", "hello🎉world")`, 7},

		// Start position with Unicode
		{"Start position skips CJK", `FIND("本", "日本語日本語", 4)`, 5},

		// Edge cases
		{"Empty search returns 1", `FIND("", "abc")`, 1},
		{"Empty haystack returns 0", `FIND("a", "")`, 0},
		{"Multi-byte not found", `FIND("zzz", "日本語")`, 0},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := e.Evaluate(tt.formula, nil)
			assert.NoError(t, err)
			assert.Equal(t, tt.expected, result)
		})
	}
}

func TestISBLANK_SalesforceSemantics(t *testing.T) {
	e := NewEngine()

	tests := []struct {
		name     string
		formula  string
		expected interface{}
	}{
		{"empty string is blank", `ISBLANK("")`, true},
		{"non-empty is not blank", `ISBLANK("hello")`, false},

		// Salesforce parity: whitespace-only strings are NOT blank.
		// SF ISBLANK checks null/empty only, not TrimSpace.
		{"spaces are NOT blank", `ISBLANK("  ")`, false},
		{"tab is NOT blank", `ISBLANK("	")`, false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := e.Evaluate(tt.formula, nil)
			assert.NoError(t, err)
			assert.Equal(t, tt.expected, result)
		})
	}
}
