// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// MessagesReportRequest represents TL type `messages.report#fc78af9b`.
// Report a message in a chat for violation of telegram's Terms of Service
//
// See https://core.telegram.org/method/messages.report for reference.
type MessagesReportRequest struct {
	// Peer
	Peer InputPeerClass
	// IDs of messages to report
	ID []int
	// Menu option, intially empty
	Option []byte
	// Comment for report moderation
	Message string
}

// MessagesReportRequestTypeID is TL type id of MessagesReportRequest.
const MessagesReportRequestTypeID = 0xfc78af9b

// Ensuring interfaces in compile-time for MessagesReportRequest.
var (
	_ bin.Encoder     = &MessagesReportRequest{}
	_ bin.Decoder     = &MessagesReportRequest{}
	_ bin.BareEncoder = &MessagesReportRequest{}
	_ bin.BareDecoder = &MessagesReportRequest{}
)

func (r *MessagesReportRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.ID == nil) {
		return false
	}
	if !(r.Option == nil) {
		return false
	}
	if !(r.Message == "") {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (r *MessagesReportRequest) String() string {
	if r == nil {
		return "MessagesReportRequest(nil)"
	}
	type Alias MessagesReportRequest
	return fmt.Sprintf("MessagesReportRequest%+v", Alias(*r))
}

// FillFrom fills MessagesReportRequest from given interface.
func (r *MessagesReportRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetID() (value []int)
	GetOption() (value []byte)
	GetMessage() (value string)
}) {
	r.Peer = from.GetPeer()
	r.ID = from.GetID()
	r.Option = from.GetOption()
	r.Message = from.GetMessage()
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*MessagesReportRequest) TypeID() uint32 {
	return MessagesReportRequestTypeID
}

// TypeName returns name of type in TL schema.
func (*MessagesReportRequest) TypeName() string {
	return "messages.report"
}

// TypeInfo returns info about TL type.
func (r *MessagesReportRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.report",
		ID:   MessagesReportRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Option",
			SchemaName: "option",
		},
		{
			Name:       "Message",
			SchemaName: "message",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *MessagesReportRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.report#fc78af9b as nil")
	}
	b.PutID(MessagesReportRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReportRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.report#fc78af9b as nil")
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode messages.report#fc78af9b: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.report#fc78af9b: field peer: %w", err)
	}
	b.PutVectorHeader(len(r.ID))
	for _, v := range r.ID {
		b.PutInt(v)
	}
	b.PutBytes(r.Option)
	b.PutString(r.Message)
	return nil
}

// Decode implements bin.Decoder.
func (r *MessagesReportRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.report#fc78af9b to nil")
	}
	if err := b.ConsumeID(MessagesReportRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.report#fc78af9b: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReportRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.report#fc78af9b to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.report#fc78af9b: field peer: %w", err)
		}
		r.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.report#fc78af9b: field id: %w", err)
		}

		if headerLen > 0 {
			r.ID = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode messages.report#fc78af9b: field id: %w", err)
			}
			r.ID = append(r.ID, value)
		}
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode messages.report#fc78af9b: field option: %w", err)
		}
		r.Option = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.report#fc78af9b: field message: %w", err)
		}
		r.Message = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (r *MessagesReportRequest) GetPeer() (value InputPeerClass) {
	if r == nil {
		return
	}
	return r.Peer
}

// GetID returns value of ID field.
func (r *MessagesReportRequest) GetID() (value []int) {
	if r == nil {
		return
	}
	return r.ID
}

// GetOption returns value of Option field.
func (r *MessagesReportRequest) GetOption() (value []byte) {
	if r == nil {
		return
	}
	return r.Option
}

// GetMessage returns value of Message field.
func (r *MessagesReportRequest) GetMessage() (value string) {
	if r == nil {
		return
	}
	return r.Message
}

// MessagesReport invokes method messages.report#fc78af9b returning error if any.
// Report a message in a chat for violation of telegram's Terms of Service
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//	400 OPTION_INVALID: Invalid option selected.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.report for reference.
func (c *Client) MessagesReport(ctx context.Context, request *MessagesReportRequest) (ReportResultClass, error) {
	var result ReportResultBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return result.ReportResult, nil
}
