package metadata

import "testing"

func TestResolveVirtualRelatedList_QuoteLineItemsNoPlatformTenantResolution(t *testing.T) {
	ResetTenantRelatedListRulesForTest()
	if resolution := ResolveVirtualRelatedList("quotelineitem", "QuoteId"); resolution != nil {
		t.Fatalf("QuoteLineItem tenant remaps must be authored by tenant metadata, got %+v", resolution)
	}
}

func TestResolveVirtualRelatedList_NoPlatformTenantResolutionWithoutOverlay(t *testing.T) {
	ResetTenantRelatedListRulesForTest()
	orderResolution := ResolveVirtualRelatedList("OrderItem", "OrderId")
	if orderResolution != nil {
		t.Fatalf("Order Line Items tenant remap must not be active without tenant overlay, got %+v", orderResolution)
	}
}

func TestResolveVirtualRelatedList_OrderLineItemsUsesOrderProduct(t *testing.T) {
	ResetTenantRelatedListRulesForTest()
	defer ResetTenantRelatedListRulesForTest()
	if err := RegisterTenantRelatedListRules([]RelatedListRule{
		{
			Key:             "order-line-items",
			ParentObjectAPI: "Order",
			ChildObjectAPI:  "OrderItem",
			LookupField:     "OrderId",
			Bucket:          BucketRecommended,
			SortOrder:       20,
			Resolution: &RelatedListResolution{
				Query:  ResolvedQueryTarget{ObjectApiName: "order_product__c", LookupField: "Order__c"},
				Detail: ResolvedDetailTarget{ObjectApiName: "order_product__c"},
				List:   ResolvedListTarget{ObjectApiName: "order_product__c", FilterField: "Order__c"},
				Create: ResolvedCreateTarget{Kind: "standard", ObjectApiName: "order_product__c"},
			},
		},
	}); err != nil {
		t.Fatalf("RegisterTenantRelatedListRules() error = %v", err)
	}

	resolution := ResolveVirtualRelatedList("orderitem", "OrderId")
	if resolution == nil {
		t.Fatal("expected Order Line Items virtual resolution")
	}

	if resolution.Query.ObjectApiName != "order_product__c" {
		t.Fatalf("query object = %q, want %q", resolution.Query.ObjectApiName, "order_product__c")
	}
	if resolution.Query.LookupField != "Order__c" {
		t.Fatalf("query lookup = %q, want %q", resolution.Query.LookupField, "Order__c")
	}
	if resolution.List.ObjectApiName != "order_product__c" {
		t.Fatalf("list object = %q, want %q", resolution.List.ObjectApiName, "order_product__c")
	}
	if resolution.List.FilterField != "Order__c" {
		t.Fatalf("list lookup = %q, want %q", resolution.List.FilterField, "Order__c")
	}
	if resolution.Detail.ObjectApiName != "order_product__c" {
		t.Fatalf("detail object = %q, want %q", resolution.Detail.ObjectApiName, "order_product__c")
	}
	if resolution.Create.Kind != "standard" {
		t.Fatalf("create kind = %q, want %q", resolution.Create.Kind, "standard")
	}
	if resolution.Create.ObjectApiName != "order_product__c" {
		t.Fatalf("create object = %q, want %q", resolution.Create.ObjectApiName, "order_product__c")
	}
}

func TestResolveVirtualRelatedList_OrderInvoiceCollectionsUsesOrderAccount(t *testing.T) {
	ResetTenantRelatedListRulesForTest()
	defer ResetTenantRelatedListRulesForTest()
	if err := RegisterTenantRelatedListRules([]RelatedListRule{
		{
			Key:             "order-invoice-collections",
			ParentObjectAPI: "Order",
			ChildObjectAPI:  "invoice_collection__c",
			LookupField:     "Order__c",
			Bucket:          BucketRecommended,
			SortOrder:       40,
			Resolution: &RelatedListResolution{
				Query:  ResolvedQueryTarget{ObjectApiName: "invoice_collection__c", LookupField: "Account__c", ParentFieldApiName: "AccountId"},
				Detail: ResolvedDetailTarget{ObjectApiName: "invoice_collection__c"},
				List:   ResolvedListTarget{ObjectApiName: "invoice_collection__c", FilterField: "Account__c", ParentFieldApiName: "AccountId"},
				Create: ResolvedCreateTarget{Kind: "none"},
			},
		},
	}); err != nil {
		t.Fatalf("RegisterTenantRelatedListRules() error = %v", err)
	}

	resolution := ResolveVirtualRelatedList("invoice_collection__c", "Order__c")
	if resolution == nil {
		t.Fatal("expected Order Invoice Collections virtual resolution")
	}

	if resolution.Query.ObjectApiName != "invoice_collection__c" {
		t.Fatalf("query object = %q, want %q", resolution.Query.ObjectApiName, "invoice_collection__c")
	}
	if resolution.Query.LookupField != "Account__c" {
		t.Fatalf("query lookup = %q, want %q", resolution.Query.LookupField, "Account__c")
	}
	if resolution.Query.ParentFieldApiName != "AccountId" {
		t.Fatalf("query parent field = %q, want %q", resolution.Query.ParentFieldApiName, "AccountId")
	}
	if resolution.List.FilterField != "Account__c" {
		t.Fatalf("list filter = %q, want %q", resolution.List.FilterField, "Account__c")
	}
	if resolution.List.ParentFieldApiName != "AccountId" {
		t.Fatalf("list parent field = %q, want %q", resolution.List.ParentFieldApiName, "AccountId")
	}
	if resolution.Create.Kind != "none" {
		t.Fatalf("create kind = %q, want %q", resolution.Create.Kind, "none")
	}
}
