package models

import (
	"testing"

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

func TestRehydratePolymorphicDiscriminatorFieldsLocksGeneratedCompanion(t *testing.T) {
	schema := &ObjectMetadata{
		APIName: "CustomProbe__c",
		Fields: []FieldMetadata{
			{
				APIName:       "RelatedTo__c",
				Type:          constants.FieldTypeReference,
				ReferenceTo:   []string{"Account", "Opportunity"},
				IsPolymorphic: true,
				Createable:    true,
				Updateable:    true,
			},
			{
				APIName:    "RelatedTo__c_type",
				Type:       constants.FieldTypeString,
				Createable: true,
				Updateable: true,
			},
			{
				APIName:    "notification_type",
				Type:       constants.FieldTypeString,
				Createable: true,
				Updateable: true,
			},
		},
	}

	RehydratePolymorphicDiscriminatorFields(schema)

	got := schema.Fields[1]
	if got.GeneratedForPolymorphicReference != "RelatedTo__c" {
		t.Fatalf("GeneratedForPolymorphicReference = %q, want RelatedTo__c", got.GeneratedForPolymorphicReference)
	}
	if got.Createable || got.Updateable {
		t.Fatalf("generated discriminator Createable/Updateable = %v/%v, want false/false", got.Createable, got.Updateable)
	}

	suffixOnly := schema.Fields[2]
	if suffixOnly.GeneratedForPolymorphicReference != "" {
		t.Fatalf("suffix-only field GeneratedForPolymorphicReference = %q, want empty", suffixOnly.GeneratedForPolymorphicReference)
	}
	if !suffixOnly.Createable || !suffixOnly.Updateable {
		t.Fatalf("suffix-only field Createable/Updateable = %v/%v, want true/true", suffixOnly.Createable, suffixOnly.Updateable)
	}
}
