package analytics

import (
	"testing"

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

// TestExtractLookupDisplayNamePassesSchemaToAuth verifies that the analytics
// caller wiring passes the resolved schema through to the auth substrate
// rather than only the API name string. This proves F1: a buggy implementation
// that drops the schema arg would fail this test.
func TestExtractLookupDisplayNamePassesSchemaToAuth(t *testing.T) {
	// Issue schema from the Jira-like pack: Subject is IsNameField, no Name field.
	issueSchema := &models.ObjectMetadata{
		APIName: "Issue",
		Fields: []models.FieldMetadata{
			{APIName: "Subject", Type: constants.FieldTypeString, IsNameField: true},
			{APIName: "Description", Type: constants.FieldTypeTextarea},
		},
	}

	t.Run("IsNameField value returned", func(t *testing.T) {
		rec := models.SObject{"Subject": "Fix login", "Id": "rec-1"}
		got := extractLookupDisplayName(issueSchema, rec)
		if got != "Fix login" {
			t.Fatalf("expected %q, got %q", "Fix login", got)
		}
	})

	t.Run("blank IsNameField returns empty", func(t *testing.T) {
		rec := models.SObject{"Subject": "", "Id": "rec-2"}
		got := extractLookupDisplayName(issueSchema, rec)
		if got != "" {
			t.Fatalf("expected empty string, got %q", got)
		}
	})

	t.Run("nil schema returns empty", func(t *testing.T) {
		rec := models.SObject{"Subject": "Fix login"}
		got := extractLookupDisplayName(nil, rec)
		if got != "" {
			t.Fatalf("expected empty string for nil schema, got %q", got)
		}
	})
}
