package tron

import (
	"math/big"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestToBaseUnits(t *testing.T) {
	tests := []struct {
		name      string
		amount    string
		decimals  *big.Int
		want      string
		expectErr bool
	}{
		{
			name:     "fractional amount",
			amount:   "0.7",
			decimals: big.NewInt(6),
			want:     "700000",
		},
		{
			name:     "decimal amount",
			amount:   "9.3",
			decimals: big.NewInt(6),
			want:     "9300000",
		},
		{
			name:     "integer amount",
			amount:   "12",
			decimals: big.NewInt(6),
			want:     "12000000",
		},
		{
			name:     "lowest unit",
			amount:   "0.000001",
			decimals: big.NewInt(6),
			want:     "1",
		},
		{
			name:      "exceeds precision",
			amount:    "0.0000001",
			decimals:  big.NewInt(6),
			expectErr: true,
		},
		{
			name:      "missing decimals",
			amount:    "1",
			decimals:  nil,
			expectErr: true,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			result, err := toBaseUnits(test.amount, test.decimals)
			if test.expectErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
			require.Equal(t, test.want, result.String())
		})
	}
}
