package merrors

import (
	"errors"
	"strings"
	"testing"
)

func TestInvalidArgumentSupportsBrokenArgumentName(t *testing.T) {
	t.Run("without argument name keeps old behavior", func(t *testing.T) {
		err := InvalidArgument("value is missing")
		expected := "invalidArgError: value is missing"
		if err.Error() != expected {
			t.Fatalf("unexpected error message: %s", err)
		}
		if !errors.Is(err, ErrInvalidArg) {
			t.Fatalf("error should wrap ErrInvalidArg")
		}
	})

	t.Run("single argument name", func(t *testing.T) {
		err := InvalidArgument("value is missing", "bot_token_env")
		expected := `invalidArgError: broken argument "bot_token_env": value is missing`
		if err.Error() != expected {
			t.Fatalf("unexpected error message: %s", err)
		}
	})

	t.Run("multiple argument names", func(t *testing.T) {
		err := InvalidArgument("value mismatch", "bot_token_env", "chat_id_env", " ")
		expected := `invalidArgError: broken arguments "bot_token_env", "chat_id_env": value mismatch`
		if err.Error() != expected {
			t.Fatalf("unexpected error message: %s", err)
		}
	})
}

func TestInvalidArgumentWrapSupportsBrokenArgumentName(t *testing.T) {
	base := errors.New("root cause")
	err := InvalidArgumentWrap(base, "value is missing", "bot_token_env")
	if !strings.Contains(err.Error(), `invalidArgError: broken argument "bot_token_env": value is missing`) {
		t.Fatalf("wrapped error should include broken argument name: %s", err)
	}
	if !errors.Is(err, ErrInvalidArg) || !errors.Is(err, base) {
		t.Fatalf("wrapped error should preserve all error layers")
	}
}
