package shared

import (
	"github.com/shopspring/decimal"
	connectorv1 "github.com/tech/sendico/pkg/proto/connector/v1"
	chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
)

// GasTopUpRule defines buffer, minimum, rounding, and cap behavior for native gas top-ups.
type GasTopUpRule struct {
	BufferPercent    decimal.Decimal
	MinNativeBalance decimal.Decimal
	RoundingUnit     decimal.Decimal
	MaxTopUp         decimal.Decimal
}

// GasTopUpPolicy captures default and optional overrides for native vs contract transfers.
type GasTopUpPolicy struct {
	Default  GasTopUpRule
	Native   *GasTopUpRule
	Contract *GasTopUpRule
}

// Rule selects the policy rule for the transfer type.
func (p *GasTopUpPolicy) Rule(contractTransfer bool) (GasTopUpRule, bool) {
	if p == nil {
		return GasTopUpRule{}, false
	}
	if contractTransfer && p.Contract != nil {
		return *p.Contract, true
	}
	if !contractTransfer && p.Native != nil {
		return *p.Native, true
	}
	return p.Default, true
}

func СhainTransferStatusToOperation(ts chainv1.TransferStatus) connectorv1.OperationStatus {
	switch ts {
	case chainv1.TransferStatus_TRANSFER_CANCELLED:
		return connectorv1.OperationStatus_OPERATION_CANCELLED
	case chainv1.TransferStatus_TRANSFER_CREATED:
		return connectorv1.OperationStatus_OPERATION_CREATED
	case chainv1.TransferStatus_TRANSFER_FAILED:
		return connectorv1.OperationStatus_OPERATION_FAILED
	case chainv1.TransferStatus_TRANSFER_SUCCESS:
		return connectorv1.OperationStatus_OPERATION_SUCCESS
	case chainv1.TransferStatus_TRANSFER_WAITING:
		return connectorv1.OperationStatus_OPERATION_WAITING
	default:
	}
	return connectorv1.OperationStatus_OPERATION_STATUS_UNSPECIFIED
}
