package evm

import (
	"fmt"
	"strings"

	"github.com/shopspring/decimal"
	"github.com/tech/sendico/gateway/tron/shared"
	"github.com/tech/sendico/gateway/tron/storage/model"
	"github.com/tech/sendico/pkg/merrors"
	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
)

var evmBaseUnitFactor = decimal.NewFromInt(1_000_000_000_000_000_000)

// ComputeGasTopUp applies the network policy to decide an EVM native-token top-up amount.
func ComputeGasTopUp(network shared.Network, wallet *model.ManagedWallet, estimatedFee *moneyv1.Money, currentBalance *moneyv1.Money) (*moneyv1.Money, bool, error) {
	if wallet == nil {
		return nil, false, merrors.InvalidArgument("wallet is required")
	}
	if estimatedFee == nil || strings.TrimSpace(estimatedFee.GetAmount()) == "" || strings.TrimSpace(estimatedFee.GetCurrency()) == "" {
		return nil, false, merrors.InvalidArgument("estimated fee is required")
	}
	if currentBalance == nil || strings.TrimSpace(currentBalance.GetAmount()) == "" || strings.TrimSpace(currentBalance.GetCurrency()) == "" {
		return nil, false, merrors.InvalidArgument("current native balance is required")
	}
	if network.GasTopUpPolicy == nil {
		return nil, false, merrors.InvalidArgument("gas top-up policy is not configured")
	}

	nativeCurrency := strings.TrimSpace(network.NativeToken)
	if nativeCurrency == "" {
		nativeCurrency = strings.ToUpper(network.Name.String())
	}
	if !strings.EqualFold(nativeCurrency, estimatedFee.GetCurrency()) {
		return nil, false, merrors.InvalidArgument(fmt.Sprintf("estimated fee currency mismatch (expected %s)", nativeCurrency))
	}
	if !strings.EqualFold(nativeCurrency, currentBalance.GetCurrency()) {
		return nil, false, merrors.InvalidArgument(fmt.Sprintf("native balance currency mismatch (expected %s)", nativeCurrency))
	}

	estimatedNative, err := evmToNative(estimatedFee)
	if err != nil {
		return nil, false, err
	}
	currentNative, err := evmToNative(currentBalance)
	if err != nil {
		return nil, false, err
	}

	isContract := strings.TrimSpace(wallet.ContractAddress) != ""
	rule, ok := network.GasTopUpPolicy.Rule(isContract)
	if !ok {
		return nil, false, merrors.InvalidArgument("gas top-up policy is not configured")
	}
	if rule.RoundingUnit.LessThanOrEqual(decimal.Zero) {
		return nil, false, merrors.InvalidArgument("gas top-up rounding unit must be > 0")
	}
	if rule.MaxTopUp.LessThanOrEqual(decimal.Zero) {
		return nil, false, merrors.InvalidArgument("gas top-up max top-up must be > 0")
	}

	required := estimatedNative.Sub(currentNative)
	if required.IsNegative() {
		required = decimal.Zero
	}
	bufferedRequired := required.Mul(decimal.NewFromInt(1).Add(rule.BufferPercent))

	minBalanceTopUp := rule.MinNativeBalance.Sub(currentNative)
	if minBalanceTopUp.IsNegative() {
		minBalanceTopUp = decimal.Zero
	}

	rawTopUp := bufferedRequired
	if minBalanceTopUp.GreaterThan(rawTopUp) {
		rawTopUp = minBalanceTopUp
	}

	roundedTopUp := decimal.Zero
	if rawTopUp.IsPositive() {
		roundedTopUp = rawTopUp.Div(rule.RoundingUnit).Ceil().Mul(rule.RoundingUnit)
	}

	topUp := roundedTopUp
	capHit := false
	if topUp.GreaterThan(rule.MaxTopUp) {
		topUp = rule.MaxTopUp
		capHit = true
	}

	if !topUp.IsPositive() {
		return nil, capHit, nil
	}

	baseUnits := topUp.Mul(evmBaseUnitFactor).Ceil().Truncate(0)
	return &moneyv1.Money{
		Currency: strings.ToUpper(nativeCurrency),
		Amount:   baseUnits.StringFixed(0),
	}, capHit, nil
}

func evmToNative(amount *moneyv1.Money) (decimal.Decimal, error) {
	value, err := decimal.NewFromString(strings.TrimSpace(amount.GetAmount()))
	if err != nil {
		return decimal.Zero, err
	}
	return value.Div(evmBaseUnitFactor), nil
}
