package types_test

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/require"

	sdk "git.cw.tr/mukan-network/mukan-sdk/types"
	moduletestutil "git.cw.tr/mukan-network/mukan-sdk/types/module/testutil"

	ibc "git.cw.tr/mukan-network/mukan-ibc/modules/core"
	"git.cw.tr/mukan-network/mukan-ibc/modules/core/03-connection/types"
)

func TestCodecTypeRegistration(t *testing.T) {
	testCases := []struct {
		name     string
		typeURL  string
		expError error
	}{
		{
			"success: MsgConnectionOpenInit",
			sdk.MsgTypeURL(&types.MsgConnectionOpenInit{}),
			nil,
		},
		{
			"success: MsgConnectionOpenTry",
			sdk.MsgTypeURL(&types.MsgConnectionOpenTry{}),
			nil,
		},
		{
			"success: MsgConnectionOpenAck",
			sdk.MsgTypeURL(&types.MsgConnectionOpenAck{}),
			nil,
		},
		{
			"success: MsgConnectionOpenConfirm",
			sdk.MsgTypeURL(&types.MsgConnectionOpenConfirm{}),
			nil,
		},
		{
			"success: MsgUpdateParams",
			sdk.MsgTypeURL(&types.MsgUpdateParams{}),
			nil,
		},
		{
			"type not registered on codec",
			"ibc.invalid.MsgTypeURL",
			errors.New("unable to resolve type URL ibc.invalid.MsgTypeURL"),
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			encodingCfg := moduletestutil.MakeTestEncodingConfig(ibc.AppModuleBasic{})
			msg, err := encodingCfg.Codec.InterfaceRegistry().Resolve(tc.typeURL)

			if tc.expError == nil {
				require.NotNil(t, msg)
				require.NoError(t, err)
			} else {
				require.Nil(t, msg)
				require.ErrorContains(t, err, tc.expError.Error())
			}
		})
	}
}
