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

// AccountDisablePeerConnectedBotRequest represents TL type `account.disablePeerConnectedBot#5e437ed9`.
// Permanently disconnect a specific chat from all business bots »¹ (equivalent to
// specifying it in recipients.exclude_users during initial configuration with account
// updateConnectedBot »²); to reconnect of a chat disconnected using this method the
// user must reconnect the entire bot by invoking account.updateConnectedBot »³.
//
// Links:
//  1. https://core.telegram.org/api/bots/connected-business-bots
//  2. https://core.telegram.org/method/account.updateConnectedBot
//  3. https://core.telegram.org/method/account.updateConnectedBot
//
// See https://core.telegram.org/method/account.disablePeerConnectedBot for reference.
type AccountDisablePeerConnectedBotRequest struct {
	// The chat to disconnect
	Peer InputPeerClass
}

// AccountDisablePeerConnectedBotRequestTypeID is TL type id of AccountDisablePeerConnectedBotRequest.
const AccountDisablePeerConnectedBotRequestTypeID = 0x5e437ed9

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

func (d *AccountDisablePeerConnectedBotRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Peer == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountDisablePeerConnectedBotRequest from given interface.
func (d *AccountDisablePeerConnectedBotRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
}) {
	d.Peer = from.GetPeer()
}

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

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

// TypeInfo returns info about TL type.
func (d *AccountDisablePeerConnectedBotRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.disablePeerConnectedBot",
		ID:   AccountDisablePeerConnectedBotRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *AccountDisablePeerConnectedBotRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode account.disablePeerConnectedBot#5e437ed9 as nil")
	}
	b.PutID(AccountDisablePeerConnectedBotRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *AccountDisablePeerConnectedBotRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode account.disablePeerConnectedBot#5e437ed9 as nil")
	}
	if d.Peer == nil {
		return fmt.Errorf("unable to encode account.disablePeerConnectedBot#5e437ed9: field peer is nil")
	}
	if err := d.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.disablePeerConnectedBot#5e437ed9: field peer: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *AccountDisablePeerConnectedBotRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode account.disablePeerConnectedBot#5e437ed9 to nil")
	}
	if err := b.ConsumeID(AccountDisablePeerConnectedBotRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.disablePeerConnectedBot#5e437ed9: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *AccountDisablePeerConnectedBotRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode account.disablePeerConnectedBot#5e437ed9 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.disablePeerConnectedBot#5e437ed9: field peer: %w", err)
		}
		d.Peer = value
	}
	return nil
}

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

// AccountDisablePeerConnectedBot invokes method account.disablePeerConnectedBot#5e437ed9 returning error if any.
// Permanently disconnect a specific chat from all business bots »¹ (equivalent to
// specifying it in recipients.exclude_users during initial configuration with account
// updateConnectedBot »²); to reconnect of a chat disconnected using this method the
// user must reconnect the entire bot by invoking account.updateConnectedBot »³.
//
// Links:
//  1. https://core.telegram.org/api/bots/connected-business-bots
//  2. https://core.telegram.org/method/account.updateConnectedBot
//  3. https://core.telegram.org/method/account.updateConnectedBot
//
// Possible errors:
//
//	400 BOT_ALREADY_DISABLED: The connected business bot was already disabled for the specified peer.
//	400 BOT_NOT_CONNECTED_YET: No business bot is connected to the currently logged in user.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/account.disablePeerConnectedBot for reference.
func (c *Client) AccountDisablePeerConnectedBot(ctx context.Context, peer InputPeerClass) (bool, error) {
	var result BoolBox

	request := &AccountDisablePeerConnectedBotRequest{
		Peer: peer,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
