package tenantoverlay

import (
	"encoding/json"
	"testing"

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

// TestTechnicalValidationFlow_ShippedFlowUsesCorrectObject validates that the
// shipped tenant overlay flow for Technical_Validation_Is_Request_Products uses
// the standard OpportunityLineItem object (not the mismatched Opportunity_Product__c).
// This guards against the object name mismatch that caused all Opportunity stage
// transitions to Technical Validation / Business Validation / Negotiation / Deal
// Closed to fail with "Object not found" — blocking the entire sales cycle.
func TestTechnicalValidationFlow_ShippedFlowUsesCorrectObject(t *testing.T) {
	raw := readTestOverlayFile(t, "flows/Technical_Validation_Is_Request_Products.flow.json")

	var meta models.FlowMetadata
	if err := json.Unmarshal(raw, &meta); err != nil {
		t.Fatalf("parse flow JSON: %v", err)
	}

	if len(meta.RecordLookups) == 0 {
		t.Fatal("flow has no RecordLookup elements")
	}
	lookup := meta.RecordLookups[0]
	if lookup.Object != "OpportunityLineItem" {
		t.Errorf("RecordLookup.Object = %q, want OpportunityLineItem (Opportunity_Product__c is not a platform object and causes Object-not-found errors at runtime)",
			lookup.Object)
	}
	if len(lookup.Filters) == 0 {
		t.Fatal("RecordLookup has no filters")
	}
	if lookup.Filters[0].Field != "OpportunityId" {
		t.Errorf("RecordLookup.Filters[0].Field = %q, want OpportunityId (standard FK from OpportunityLineItem to Opportunity)",
			lookup.Filters[0].Field)
	}
}
