package chain

import (
	"testing"

	"github.com/stretchr/testify/require"
	chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
)

func TestNetworkName(t *testing.T) {
	require.Equal(t, "TRON_MAINNET", NetworkName(chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET))
	require.Equal(t, "", NetworkName(chainv1.ChainNetwork_CHAIN_NETWORK_UNSPECIFIED))
}

func TestNetworkAlias(t *testing.T) {
	require.Equal(t, "ETHEREUM_MAINNET", NetworkAlias(chainv1.ChainNetwork_CHAIN_NETWORK_ETHEREUM_MAINNET))
	require.Equal(t, "TRON_MAINNET", NetworkAlias(chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET))
	require.Equal(t, "TRON_NILE", NetworkAlias(chainv1.ChainNetwork_CHAIN_NETWORK_TRON_NILE))
	require.Equal(t, "ARBITRUM_ONE", NetworkAlias(chainv1.ChainNetwork_CHAIN_NETWORK_ARBITRUM_ONE))
	require.Equal(t, "ARBITRUM_SEPOLIA", NetworkAlias(chainv1.ChainNetwork_CHAIN_NETWORK_ARBITRUM_SEPOLIA))
	require.Equal(t, "UNSPECIFIED", NetworkAlias(chainv1.ChainNetwork_CHAIN_NETWORK_UNSPECIFIED))
}

func TestNetworkFromString(t *testing.T) {
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, NetworkFromString("TRON_MAINNET"))
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, NetworkFromString("CHAIN_NETWORK_TRON_MAINNET"))
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, NetworkFromString("tron mainnet"))
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, NetworkFromString("tron-mainnet"))
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, NetworkFromString("TRON_MAINNET"))
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_ETHEREUM_MAINNET, NetworkFromString("ETH"))
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_ARBITRUM_ONE, NetworkFromString("ARBITRUM"))
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_UNSPECIFIED, NetworkFromString(""))
}

func TestAssetString(t *testing.T) {
	asset := &chainv1.Asset{
		Chain:       chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET,
		TokenSymbol: "usdt",
	}
	require.Equal(t, "USDT-TRC20", AssetString(asset))
}

func TestParseAsset(t *testing.T) {
	asset, err := ParseAsset("USDT-TRC20", "TRON_MAINNET", "", "")
	require.NoError(t, err)
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, asset.GetChain())
	require.Equal(t, "USDT", asset.GetTokenSymbol())
	require.Equal(t, "", asset.GetContractAddress())

	contract := "TR7NhQjeKQxGTCi8q8ZY4pL8otSzgjLj6T"
	asset, err = ParseAsset("USDT-TRC20", "TRON_MAINNET", "", contract)
	require.NoError(t, err)
	require.Equal(t, contract, asset.GetContractAddress())

	asset, err = ParseAsset("USDT-TRC20", "CHAIN_NETWORK_TRON_MAINNET", "", "")
	require.NoError(t, err)
	require.Equal(t, chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, asset.GetChain())

	_, err = ParseAsset("USDT-TRC20", "", "USDT", "")
	require.Error(t, err)
}
