package wire

import (
	"testing"

	sharedErrors "github.com/nexuscrm/shared/pkg/errors"
)

// Phase-2 L0-B PR-0b (goldens-first) capture-and-freeze regression.
//
// This test pins the EXACT current wire bytes produced by the dialect copy of
// standardActionEndpointV63 / CreateFromSourceGuardMessageV63 (wire.go). It is the
// byte-for-byte twin of the kernel golden (nexuscrm-kernel/pkg/apierr/
// standard_action_endpoint_golden_test.go): the dialect copy and the kernel copy
// MUST emit identical "/services/data/vXX.X/actions/standard/<action>" URL grammar
// and surrounding guard message. L0-B's carve relocates the kernel copy; this
// golden guarantees the dialect copy keeps producing these same bytes.
//
// The goldens are inline frozen literals (the producers are fully deterministic
// pure-string transforms — there is no nondeterminism to absorb into a file). The
// empty-version case pins the v63.0 fallback the dialect applies for a blank
// apiVersion.
//
// ZERO production change: this file only reads the existing exported/unexported
// producers in this package and asserts equality. It modifies no production .go.

// Frozen wire bytes for the representative action + api version v63.0.
const (
	goldenStandardActionEndpointV63 = "/services/data/v63.0/actions/standard/Case.Escalate"

	goldenCreateFromSourceGuardSingleV63 = "Direct create of Case with a configured source-FK populated is not permitted. " +
		"Invoke the owning standard action endpoint instead: " +
		"/services/data/v63.0/actions/standard/Case.Escalate"

	goldenCreateFromSourceGuardMultiV63 = "Direct create of Case with a configured source-FK populated is not permitted. " +
		"Invoke one of the owning standard action endpoints instead: " +
		"/services/data/v63.0/actions/standard/Case.Escalate, " +
		"/services/data/v63.0/actions/standard/Lead.Convert"
)

// TestStandardActionEndpointV63GoldenDialect freezes the dialect copy's URL bytes.
func TestStandardActionEndpointV63GoldenDialect(t *testing.T) {
	// Bare endpoint URL byte-golden (single action).
	if got := standardActionEndpointV63("v63.0", "Case.Escalate"); got != goldenStandardActionEndpointV63 {
		t.Errorf("standardActionEndpointV63 drifted from frozen wire bytes:\n got: %q\nwant: %q", got, goldenStandardActionEndpointV63)
	}

	// Empty api version must fall back to v63.0 (current dialect behavior).
	if got := standardActionEndpointV63("", "Case.Escalate"); got != goldenStandardActionEndpointV63 {
		t.Errorf("standardActionEndpointV63 empty-version fallback drifted:\n got: %q\nwant: %q", got, goldenStandardActionEndpointV63)
	}

	// Single-action guard message byte-golden.
	single := &sharedErrors.CreateFromSourceGuardDetails{
		ObjectName:  "Case",
		ActionNames: []string{"Case.Escalate"},
	}
	if got := CreateFromSourceGuardMessageV63(single, "v63.0"); got != goldenCreateFromSourceGuardSingleV63 {
		t.Errorf("CreateFromSourceGuardMessageV63 (single) drifted from frozen wire bytes:\n got: %q\nwant: %q", got, goldenCreateFromSourceGuardSingleV63)
	}

	// Multi-action guard message byte-golden. Action names are already sorted so the
	// shared dedup/sort cannot reorder them out from under the frozen value.
	multi := &sharedErrors.CreateFromSourceGuardDetails{
		ObjectName:  "Case",
		ActionNames: []string{"Case.Escalate", "Lead.Convert"},
	}
	if got := CreateFromSourceGuardMessageV63(multi, "v63.0"); got != goldenCreateFromSourceGuardMultiV63 {
		t.Errorf("CreateFromSourceGuardMessageV63 (multi) drifted from frozen wire bytes:\n got: %q\nwant: %q", got, goldenCreateFromSourceGuardMultiV63)
	}
}
