package analytics

import (
	"strings"

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

// formulaFieldPrimaryTarget resolves the authoritative target object for a
// formula relationship field. Prefer explicit metadata, but fall back to the
// platform-owned field invariants for universal pseudo-relationships like
// OwnerId and RecordTypeId when schema rows omit ReferenceTo.
func formulaFieldPrimaryTarget(field models.FieldMetadata) (string, bool) {
	for _, target := range field.ReferenceTo {
		if trimmed := strings.TrimSpace(target); trimmed != "" {
			return trimmed, true
		}
	}
	return constants.PlatformReferenceTargetForField(field.APIName)
}
