// 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{}
)

// MessagesGetFactCheckRequest represents TL type `messages.getFactCheck#b9cdc5ee`.
// Fetch one or more factchecks, see here »¹ for the full flow.
//
// Links:
//  1. https://core.telegram.org/api/factcheck
//
// See https://core.telegram.org/method/messages.getFactCheck for reference.
type MessagesGetFactCheckRequest struct {
	// Peer where the messages were sent.
	Peer InputPeerClass
	// Messages that have associated factCheck¹ constructors with the need_check flag set.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/factCheck
	MsgID []int
}

// MessagesGetFactCheckRequestTypeID is TL type id of MessagesGetFactCheckRequest.
const MessagesGetFactCheckRequestTypeID = 0xb9cdc5ee

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

func (g *MessagesGetFactCheckRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.MsgID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetFactCheckRequest from given interface.
func (g *MessagesGetFactCheckRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetMsgID() (value []int)
}) {
	g.Peer = from.GetPeer()
	g.MsgID = from.GetMsgID()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetFactCheckRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getFactCheck",
		ID:   MessagesGetFactCheckRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *MessagesGetFactCheckRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getFactCheck#b9cdc5ee as nil")
	}
	b.PutID(MessagesGetFactCheckRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetFactCheckRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getFactCheck#b9cdc5ee as nil")
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode messages.getFactCheck#b9cdc5ee: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getFactCheck#b9cdc5ee: field peer: %w", err)
	}
	b.PutVectorHeader(len(g.MsgID))
	for _, v := range g.MsgID {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *MessagesGetFactCheckRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getFactCheck#b9cdc5ee to nil")
	}
	if err := b.ConsumeID(MessagesGetFactCheckRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.getFactCheck#b9cdc5ee: %w", err)
	}
	return g.DecodeBare(b)
}

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

		if headerLen > 0 {
			g.MsgID = 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.getFactCheck#b9cdc5ee: field msg_id: %w", err)
			}
			g.MsgID = append(g.MsgID, value)
		}
	}
	return nil
}

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

// GetMsgID returns value of MsgID field.
func (g *MessagesGetFactCheckRequest) GetMsgID() (value []int) {
	if g == nil {
		return
	}
	return g.MsgID
}

// MessagesGetFactCheck invokes method messages.getFactCheck#b9cdc5ee returning error if any.
// Fetch one or more factchecks, see here »¹ for the full flow.
//
// Links:
//  1. https://core.telegram.org/api/factcheck
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.getFactCheck for reference.
func (c *Client) MessagesGetFactCheck(ctx context.Context, request *MessagesGetFactCheckRequest) ([]FactCheck, error) {
	var result FactCheckVector

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