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

// AccountGetPaidMessagesRevenueRequest represents TL type `account.getPaidMessagesRevenue#19ba4a67`.
// Get the number of stars we have received from the specified user thanks to paid
// messages »¹; the received amount will be equal to the sent amount multiplied by
// stars_paid_message_commission_permille² divided by 1000.
//
// Links:
//  1. https://core.telegram.org/api/paid-messages
//  2. https://core.telegram.org/api/config#stars-paid-message-commission-permille
//
// See https://core.telegram.org/method/account.getPaidMessagesRevenue for reference.
type AccountGetPaidMessagesRevenueRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, can contain the ID of a monoforum (channel direct messages)¹ to obtain the
	// number of stars the user has spent to send us direct messages via the channel.
	//
	// Links:
	//  1) https://core.telegram.org/api/monoforum
	//
	// Use SetParentPeer and GetParentPeer helpers.
	ParentPeer InputPeerClass
	// The user that paid to send us messages.
	UserID InputUserClass
}

// AccountGetPaidMessagesRevenueRequestTypeID is TL type id of AccountGetPaidMessagesRevenueRequest.
const AccountGetPaidMessagesRevenueRequestTypeID = 0x19ba4a67

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

func (g *AccountGetPaidMessagesRevenueRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.ParentPeer == nil) {
		return false
	}
	if !(g.UserID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountGetPaidMessagesRevenueRequest from given interface.
func (g *AccountGetPaidMessagesRevenueRequest) FillFrom(from interface {
	GetParentPeer() (value InputPeerClass, ok bool)
	GetUserID() (value InputUserClass)
}) {
	if val, ok := from.GetParentPeer(); ok {
		g.ParentPeer = val
	}

	g.UserID = from.GetUserID()
}

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

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

// TypeInfo returns info about TL type.
func (g *AccountGetPaidMessagesRevenueRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.getPaidMessagesRevenue",
		ID:   AccountGetPaidMessagesRevenueRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ParentPeer",
			SchemaName: "parent_peer",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *AccountGetPaidMessagesRevenueRequest) SetFlags() {
	if !(g.ParentPeer == nil) {
		g.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (g *AccountGetPaidMessagesRevenueRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode account.getPaidMessagesRevenue#19ba4a67 as nil")
	}
	b.PutID(AccountGetPaidMessagesRevenueRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *AccountGetPaidMessagesRevenueRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode account.getPaidMessagesRevenue#19ba4a67 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.getPaidMessagesRevenue#19ba4a67: field flags: %w", err)
	}
	if g.Flags.Has(0) {
		if g.ParentPeer == nil {
			return fmt.Errorf("unable to encode account.getPaidMessagesRevenue#19ba4a67: field parent_peer is nil")
		}
		if err := g.ParentPeer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.getPaidMessagesRevenue#19ba4a67: field parent_peer: %w", err)
		}
	}
	if g.UserID == nil {
		return fmt.Errorf("unable to encode account.getPaidMessagesRevenue#19ba4a67: field user_id is nil")
	}
	if err := g.UserID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.getPaidMessagesRevenue#19ba4a67: field user_id: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *AccountGetPaidMessagesRevenueRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode account.getPaidMessagesRevenue#19ba4a67 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.getPaidMessagesRevenue#19ba4a67: field flags: %w", err)
		}
	}
	if g.Flags.Has(0) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.getPaidMessagesRevenue#19ba4a67: field parent_peer: %w", err)
		}
		g.ParentPeer = value
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.getPaidMessagesRevenue#19ba4a67: field user_id: %w", err)
		}
		g.UserID = value
	}
	return nil
}

// SetParentPeer sets value of ParentPeer conditional field.
func (g *AccountGetPaidMessagesRevenueRequest) SetParentPeer(value InputPeerClass) {
	g.Flags.Set(0)
	g.ParentPeer = value
}

// GetParentPeer returns value of ParentPeer conditional field and
// boolean which is true if field was set.
func (g *AccountGetPaidMessagesRevenueRequest) GetParentPeer() (value InputPeerClass, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.ParentPeer, true
}

// GetUserID returns value of UserID field.
func (g *AccountGetPaidMessagesRevenueRequest) GetUserID() (value InputUserClass) {
	if g == nil {
		return
	}
	return g.UserID
}

// AccountGetPaidMessagesRevenue invokes method account.getPaidMessagesRevenue#19ba4a67 returning error if any.
// Get the number of stars we have received from the specified user thanks to paid
// messages »¹; the received amount will be equal to the sent amount multiplied by
// stars_paid_message_commission_permille² divided by 1000.
//
// Links:
//  1. https://core.telegram.org/api/paid-messages
//  2. https://core.telegram.org/api/config#stars-paid-message-commission-permille
//
// Possible errors:
//
//	400 PARENT_PEER_INVALID: The specified parent_peer is invalid.
//	400 USER_ID_INVALID: The provided user ID is invalid.
//
// See https://core.telegram.org/method/account.getPaidMessagesRevenue for reference.
func (c *Client) AccountGetPaidMessagesRevenue(ctx context.Context, request *AccountGetPaidMessagesRevenueRequest) (*AccountPaidMessagesRevenue, error) {
	var result AccountPaidMessagesRevenue

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