package types

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

const TypeMsgNewAuction = "new_auction"

var _ sdk.Msg = &MsgNewAuction{}

func NewMsgNewAuction(creator string, owner string, name string, description string) *MsgNewAuction {
	return &MsgNewAuction{
		Creator:     creator,
		Owner:       owner,
		Name:        name,
		Description: description,
	}
}

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

func (msg *MsgNewAuction) Type() string {
	return TypeMsgNewAuction
}

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

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

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