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

// AccountGetBotBusinessConnectionRequest represents TL type `account.getBotBusinessConnection#76a86270`.
// Bots may invoke this method to re-fetch the updateBotBusinessConnect¹ constructor
// associated with a specific business connection_id, see here »² for more info on
// connected business bots.
// This is needed for example for freshly logged in bots that are receiving some
// updateBotNewBusinessMessage³, etc. updates because some users have already connected
// to the bot before it could login.
// In this case, the bot is receiving messages from the business connection, but it
// hasn't cached the associated updateBotBusinessConnect⁴ with info about the
// connection (can it reply to messages? etc.) yet, and cannot receive the old ones
// because they were sent when the bot wasn't logged into the session yet.
// This method can be used to fetch info about a not-yet-cached business connection, and
// should not be invoked if the info is already cached or to fetch changes, as eventual
// changes will automatically be sent as new updateBotBusinessConnect⁵ updates to the
// bot using the usual update delivery methods »⁶.
//
// Links:
//  1. https://core.telegram.org/constructor/updateBotBusinessConnect
//  2. https://core.telegram.org/api/bots/connected-business-bots
//  3. https://core.telegram.org/constructor/updateBotNewBusinessMessage
//  4. https://core.telegram.org/constructor/updateBotBusinessConnect
//  5. https://core.telegram.org/constructor/updateBotBusinessConnect
//  6. https://core.telegram.org/api/updates
//
// See https://core.telegram.org/method/account.getBotBusinessConnection for reference.
type AccountGetBotBusinessConnectionRequest struct {
	// Business connection ID »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/connected-business-bots
	ConnectionID string
}

// AccountGetBotBusinessConnectionRequestTypeID is TL type id of AccountGetBotBusinessConnectionRequest.
const AccountGetBotBusinessConnectionRequestTypeID = 0x76a86270

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

func (g *AccountGetBotBusinessConnectionRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ConnectionID == "") {
		return false
	}

	return true
}

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

// FillFrom fills AccountGetBotBusinessConnectionRequest from given interface.
func (g *AccountGetBotBusinessConnectionRequest) FillFrom(from interface {
	GetConnectionID() (value string)
}) {
	g.ConnectionID = from.GetConnectionID()
}

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

// TypeName returns name of type in TL schema.
func (*AccountGetBotBusinessConnectionRequest) TypeName() string {
	return "account.getBotBusinessConnection"
}

// TypeInfo returns info about TL type.
func (g *AccountGetBotBusinessConnectionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.getBotBusinessConnection",
		ID:   AccountGetBotBusinessConnectionRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ConnectionID",
			SchemaName: "connection_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *AccountGetBotBusinessConnectionRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode account.getBotBusinessConnection#76a86270 as nil")
	}
	b.PutID(AccountGetBotBusinessConnectionRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *AccountGetBotBusinessConnectionRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode account.getBotBusinessConnection#76a86270 as nil")
	}
	b.PutString(g.ConnectionID)
	return nil
}

// Decode implements bin.Decoder.
func (g *AccountGetBotBusinessConnectionRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode account.getBotBusinessConnection#76a86270 to nil")
	}
	if err := b.ConsumeID(AccountGetBotBusinessConnectionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.getBotBusinessConnection#76a86270: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *AccountGetBotBusinessConnectionRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode account.getBotBusinessConnection#76a86270 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode account.getBotBusinessConnection#76a86270: field connection_id: %w", err)
		}
		g.ConnectionID = value
	}
	return nil
}

// GetConnectionID returns value of ConnectionID field.
func (g *AccountGetBotBusinessConnectionRequest) GetConnectionID() (value string) {
	if g == nil {
		return
	}
	return g.ConnectionID
}

// AccountGetBotBusinessConnection invokes method account.getBotBusinessConnection#76a86270 returning error if any.
// Bots may invoke this method to re-fetch the updateBotBusinessConnect¹ constructor
// associated with a specific business connection_id, see here »² for more info on
// connected business bots.
// This is needed for example for freshly logged in bots that are receiving some
// updateBotNewBusinessMessage³, etc. updates because some users have already connected
// to the bot before it could login.
// In this case, the bot is receiving messages from the business connection, but it
// hasn't cached the associated updateBotBusinessConnect⁴ with info about the
// connection (can it reply to messages? etc.) yet, and cannot receive the old ones
// because they were sent when the bot wasn't logged into the session yet.
// This method can be used to fetch info about a not-yet-cached business connection, and
// should not be invoked if the info is already cached or to fetch changes, as eventual
// changes will automatically be sent as new updateBotBusinessConnect⁵ updates to the
// bot using the usual update delivery methods »⁶.
//
// Links:
//  1. https://core.telegram.org/constructor/updateBotBusinessConnect
//  2. https://core.telegram.org/api/bots/connected-business-bots
//  3. https://core.telegram.org/constructor/updateBotNewBusinessMessage
//  4. https://core.telegram.org/constructor/updateBotBusinessConnect
//  5. https://core.telegram.org/constructor/updateBotBusinessConnect
//  6. https://core.telegram.org/api/updates
//
// Possible errors:
//
//	400 CONNECTION_ID_INVALID: The specified connection ID is invalid.
//
// See https://core.telegram.org/method/account.getBotBusinessConnection for reference.
func (c *Client) AccountGetBotBusinessConnection(ctx context.Context, connectionid string) (UpdatesClass, error) {
	var result UpdatesBox

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