package analytics

import "testing"

func TestSyntheticParser_Parse(t *testing.T) {
	cases := []struct {
		name   string
		in     string
		shape  syntheticShape
		base   string
		anchor string
		field  string
		second string
	}{
		// Happy paths drawn from docs/design/crt-research-data.md Q3 real examples.
		{
			name:   "scoped_custom_entity_poc_opportunity",
			in:     "CustomEntity$PoC__c@PoC__c.Opportunity__c",
			shape:  shapeCustomEntityScoped,
			base:   "PoC__c",
			anchor: "PoC__c",
			field:  "Opportunity__c",
		},
		{
			name:   "scoped_custom_entity_poc_account",
			in:     "CustomEntity$PoC__c@PoC__c.Account__c",
			shape:  shapeCustomEntityScoped,
			base:   "PoC__c",
			anchor: "PoC__c",
			field:  "Account__c",
		},
		{
			name:   "scoped_cldr_roi",
			in:     "CustomEntity$CLDR__CL_ROI__c@CLDR__CL_ROI__c.CLDR__Opportunity__c",
			shape:  shapeCustomEntityScoped,
			base:   "CLDR__CL_ROI__c",
			anchor: "CLDR__CL_ROI__c",
			field:  "CLDR__Opportunity__c",
		},
		{
			name:   "scoped_namespace_pqcrush",
			in:     "CustomEntity$pqcrush__Account_Plan__c@pqcrush__Account_Plan__c.Account__c",
			shape:  shapeCustomEntityScoped,
			base:   "pqcrush__Account_Plan__c",
			anchor: "pqcrush__Account_Plan__c",
			field:  "Account__c",
		},
		{
			name:  "bare_custom_entity_poc",
			in:    "CustomEntity$PoC__c",
			shape: shapeCustomEntity,
			base:  "PoC__c",
		},
		{
			name:  "bare_custom_entity_cldr_session",
			in:    "CustomEntity$CLDR__Call_Session__c",
			shape: shapeCustomEntity,
			base:  "CLDR__Call_Session__c",
		},
		{
			name:  "bare_custom_entity_awsapn",
			in:    "CustomEntity$awsapn__Sync_Log__c",
			shape: shapeCustomEntity,
			base:  "awsapn__Sync_Log__c",
		},
		{
			name:   "order_custom_entity_scoped_revenue_account",
			in:     "OrderCustomEntity$Revenue__c@Revenue__c.Account__c",
			shape:  shapeOrderCustomEntity,
			base:   "Revenue__c",
			anchor: "Revenue__c",
			field:  "Account__c",
		},
		{
			name:   "order_custom_entity_scoped_invoice_account",
			in:     "OrderCustomEntity$Invoice_Collection__c@Invoice_Collection__c.Account__c",
			shape:  shapeOrderCustomEntity,
			base:   "Invoice_Collection__c",
			anchor: "Invoice_Collection__c",
			field:  "Account__c",
		},
		{
			name:   "nested_custom_entity_cldr",
			in:     "CustomEntityCustomEntity$CLDR__Call_Session__c$CLDR__CallLog__c",
			shape:  shapeNestedCustomEntity,
			base:   "CLDR__Call_Session__c",
			second: "CLDR__CallLog__c",
		},
		{
			name:   "nested_custom_entity_pqcrush",
			in:     "CustomEntityCustomEntity$pqcrush__Relationship_Map__c$pqcrush__Relationship_Map_Member__c",
			shape:  shapeNestedCustomEntity,
			base:   "pqcrush__Relationship_Map__c",
			second: "pqcrush__Relationship_Map_Member__c",
		},

		// Non-matches — must return shapeUnknown.
		{name: "empty", in: "", shape: shapeUnknown},
		{name: "standard_opportunity", in: "Opportunity", shape: shapeUnknown},
		{name: "standard_lead", in: "Lead", shape: shapeUnknown},
		{name: "standard_leadlist", in: "LeadList", shape: shapeUnknown},
		{name: "real_crt_fullname", in: "Campaigns_with_Campaign_Members_and_more_fields__c", shape: shapeUnknown},
		{name: "standard_campaign_lead_lifetime", in: "CampaignLeadLifetime", shape: shapeUnknown},
		{name: "random_garbage", in: "random garbage $@.", shape: shapeUnknown},
		{name: "only_dollar", in: "$", shape: shapeUnknown},
		{name: "only_at", in: "@", shape: shapeUnknown},
		{name: "starts_with_dollar", in: "$PoC__c", shape: shapeUnknown},
		{name: "whitespace_leading", in: " CustomEntity$PoC__c", shape: shapeUnknown},
		{name: "lowercase_prefix", in: "customentity$PoC__c", shape: shapeUnknown},
		{name: "scoped_missing_anchor_field", in: "CustomEntity$PoC__c@PoC__c.", shape: shapeUnknown},
		{name: "scoped_missing_anchor_object", in: "CustomEntity$PoC__c@.Opportunity__c", shape: shapeUnknown},
	}

	p := &syntheticParser{}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := p.Parse(tc.in)
			if got.shape != tc.shape {
				t.Fatalf("shape: got %v want %v (in=%q)", got.shape, tc.shape, tc.in)
			}
			if got.baseObject != tc.base {
				t.Errorf("baseObject: got %q want %q", got.baseObject, tc.base)
			}
			if got.anchorObject != tc.anchor {
				t.Errorf("anchorObject: got %q want %q", got.anchorObject, tc.anchor)
			}
			if got.anchorField != tc.field {
				t.Errorf("anchorField: got %q want %q", got.anchorField, tc.field)
			}
			if got.secondary != tc.second {
				t.Errorf("secondary: got %q want %q", got.secondary, tc.second)
			}
		})
	}
}

func TestSyntheticParser_ZeroValueUsable(t *testing.T) {
	var p syntheticParser
	got := p.Parse("CustomEntity$PoC__c")
	if got.shape != shapeCustomEntity || got.baseObject != "PoC__c" {
		t.Fatalf("zero-value parser failed: %+v", got)
	}
}
