package proto

import (
	"fmt"

	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/model"
	chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
)

func Network2Proto(network model.ChainNetwork) (chainv1.ChainNetwork, error) {
	switch network {
	case model.ChainNetworkArbitrumOne:
		return chainv1.ChainNetwork_CHAIN_NETWORK_ARBITRUM_ONE, nil
	case model.ChainNetworkEthereumMainnet:
		return chainv1.ChainNetwork_CHAIN_NETWORK_ETHEREUM_MAINNET, nil
	case model.ChainNetworkTronMainnet:
		return chainv1.ChainNetwork_CHAIN_NETWORK_TRON_MAINNET, nil
	case model.ChainNetworkTronNile:
		return chainv1.ChainNetwork_CHAIN_NETWORK_TRON_NILE, nil
	case model.ChainNetworkUnspecified:
		return chainv1.ChainNetwork_CHAIN_NETWORK_UNSPECIFIED, nil
	default:
		return chainv1.ChainNetwork_CHAIN_NETWORK_UNSPECIFIED, merrors.InvalidArgument(fmt.Sprintf("unknown chain network value '%s'", network), "network")
	}
}

func Asset2Proto(asset *model.ChainAsset) (*chainv1.Asset, error) {
	if asset == nil {
		return nil, merrors.InvalidArgument("Asset must be provided", "asset")
	}
	netw, err := Network2Proto(asset.Chain)
	if err != nil {
		return nil, err
	}
	var contract string
	if asset.ContractAddress != nil {
		contract = *asset.ContractAddress
	}
	return &chainv1.Asset{
		Chain:           netw,
		TokenSymbol:     asset.TokenSymbol,
		ContractAddress: contract,
	}, nil
}
