package types

import (
	sdk "github.com/cosmos/cosmos-sdk/types"
	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const TypeMsgNewBid = "new_bid"

var _ sdk.Msg = &MsgNewBid{}

func NewMsgNewBid(creator string, auctionIndex string, amount string) *MsgNewBid {
	return &MsgNewBid{
		Creator:      creator,
		AuctionIndex: auctionIndex,
		Amount:       amount,
	}
}

func (msg *MsgNewBid) Route() string {
	return RouterKey
}

func (msg *MsgNewBid) Type() string {
	return TypeMsgNewBid
}

func (msg *MsgNewBid) GetSigners() []sdk.AccAddress {
	creator, err := sdk.AccAddressFromBech32(msg.Creator)
	if err != nil {
		panic(err)
	}
	return []sdk.AccAddress{creator}
}

func (msg *MsgNewBid) GetSignBytes() []byte {
	bz := ModuleCdc.MustMarshalJSON(msg)
	return sdk.MustSortJSON(bz)
}

func (msg *MsgNewBid) ValidateBasic() error {
	_, err := sdk.AccAddressFromBech32(msg.Creator)
	if err != nil {
		return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
	}
	return nil
}
