package dialectsfcrm

import (
	"testing"

	"github.com/nexuscrm/shared/pkg/constants"
	"github.com/nexuscrm/shared/pkg/models"
)

func TestRollupDescribeUsesResolvedOutputMetadata(t *testing.T) {
	currency := models.FieldType(constants.FieldTypeCurrency)
	integer := models.FieldType("int")
	precision := 18
	currencyScale := 2
	integerScale := 0

	for _, tc := range []struct {
		name       string
		returnType *models.FieldType
		scale      *int
		wantType   string
		wantScale  int
	}{
		{
			name:       "sum currency",
			returnType: &currency,
			scale:      &currencyScale,
			wantType:   "currency",
			wantScale:  2,
		},
		{
			name:       "count integer",
			returnType: &integer,
			scale:      &integerScale,
			wantType:   "int",
			wantScale:  0,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			field := models.FieldMetadata{
				APIName:    "Rollup__c",
				Type:       constants.FieldTypeRollupSummary,
				ReturnType: tc.returnType,
				Precision:  &precision,
				Scale:      tc.scale,
			}
			if got := mapFieldTypeToSF(field); got != tc.wantType {
				t.Fatalf("mapFieldTypeToSF = %q, want %q", got, tc.wantType)
			}
			if got := fieldPrecision(field); got != 18 {
				t.Fatalf("fieldPrecision = %d, want 18", got)
			}
			if got := fieldScale(field); got != tc.wantScale {
				t.Fatalf("fieldScale = %d, want %d", got, tc.wantScale)
			}
		})
	}
}
