package constants

import (
	"context"
	"testing"

	contractconstants "github.com/pingcap-inc/nexuscrm-contract/pkg/constants"
)

func TestConnectorInboundContextInteropWithContract(t *testing.T) {
	ctx := contractconstants.WithConnectorInboundFlowExecution(context.Background(), "SyncContact", "crm-pack")
	if !ConnectorInboundExecutionAuthorizesFlow(ctx, "SyncContact", "crm-pack") {
		t.Fatalf("shared wrapper did not read contract-owned connector inbound context")
	}

	ctx = WithConnectorInboundCompanionFlowExecution(context.Background(), "301xx000000001AAA")
	if !contractconstants.ConnectorInboundCompanionExecutionAuthorizesVersion(ctx, "301xx000000001AAA") {
		t.Fatalf("contract did not read shared wrapper-stamped companion context")
	}
}

func TestContextKeyInteropWithContract(t *testing.T) {
	ctx := context.WithValue(context.Background(), contractconstants.ContextKeyConnectorSyncOrigin, "crm-pack")
	if got, _ := ctx.Value(ContextKeyConnectorSyncOrigin).(string); got != "crm-pack" {
		t.Fatalf("ContextKeyConnectorSyncOrigin = %q, want crm-pack", got)
	}
}

func TestFormulaSystemContextConnectorSyncOriginUsesContractKey(t *testing.T) {
	ctx := context.WithValue(context.Background(), contractconstants.ContextKeyConnectorSyncOrigin, " HubSpot-CRM-Connector ")
	if got := ConnectorSyncOriginFromContext(ctx); got != "hubspot-crm-connector" {
		t.Fatalf("ConnectorSyncOriginFromContext = %q, want canonical connector namespace", got)
	}

	systemCtx := FormulaSystemContextFromContext(ctx)
	if got := systemCtx[FormulaSystemContextConnectorSyncOrigin]; got != "hubspot-crm-connector" {
		t.Fatalf("FormulaSystemContext connector origin = %q, want canonical connector namespace", got)
	}
}
