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

// PaymentsFulfillStarsSubscriptionRequest represents TL type `payments.fulfillStarsSubscription#cc5bebb3`.
// Re-join a private channel associated to an active Telegram Star subscription »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#paid-invite-links
//
// See https://core.telegram.org/method/payments.fulfillStarsSubscription for reference.
type PaymentsFulfillStarsSubscriptionRequest struct {
	// Always pass inputPeerSelf¹.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputPeerSelf
	Peer InputPeerClass
	// ID of the subscription.
	SubscriptionID string
}

// PaymentsFulfillStarsSubscriptionRequestTypeID is TL type id of PaymentsFulfillStarsSubscriptionRequest.
const PaymentsFulfillStarsSubscriptionRequestTypeID = 0xcc5bebb3

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

func (f *PaymentsFulfillStarsSubscriptionRequest) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Peer == nil) {
		return false
	}
	if !(f.SubscriptionID == "") {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsFulfillStarsSubscriptionRequest from given interface.
func (f *PaymentsFulfillStarsSubscriptionRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetSubscriptionID() (value string)
}) {
	f.Peer = from.GetPeer()
	f.SubscriptionID = from.GetSubscriptionID()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsFulfillStarsSubscriptionRequest) TypeName() string {
	return "payments.fulfillStarsSubscription"
}

// TypeInfo returns info about TL type.
func (f *PaymentsFulfillStarsSubscriptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.fulfillStarsSubscription",
		ID:   PaymentsFulfillStarsSubscriptionRequestTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "SubscriptionID",
			SchemaName: "subscription_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *PaymentsFulfillStarsSubscriptionRequest) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode payments.fulfillStarsSubscription#cc5bebb3 as nil")
	}
	b.PutID(PaymentsFulfillStarsSubscriptionRequestTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *PaymentsFulfillStarsSubscriptionRequest) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode payments.fulfillStarsSubscription#cc5bebb3 as nil")
	}
	if f.Peer == nil {
		return fmt.Errorf("unable to encode payments.fulfillStarsSubscription#cc5bebb3: field peer is nil")
	}
	if err := f.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.fulfillStarsSubscription#cc5bebb3: field peer: %w", err)
	}
	b.PutString(f.SubscriptionID)
	return nil
}

// Decode implements bin.Decoder.
func (f *PaymentsFulfillStarsSubscriptionRequest) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode payments.fulfillStarsSubscription#cc5bebb3 to nil")
	}
	if err := b.ConsumeID(PaymentsFulfillStarsSubscriptionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.fulfillStarsSubscription#cc5bebb3: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *PaymentsFulfillStarsSubscriptionRequest) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode payments.fulfillStarsSubscription#cc5bebb3 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.fulfillStarsSubscription#cc5bebb3: field peer: %w", err)
		}
		f.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode payments.fulfillStarsSubscription#cc5bebb3: field subscription_id: %w", err)
		}
		f.SubscriptionID = value
	}
	return nil
}

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

// GetSubscriptionID returns value of SubscriptionID field.
func (f *PaymentsFulfillStarsSubscriptionRequest) GetSubscriptionID() (value string) {
	if f == nil {
		return
	}
	return f.SubscriptionID
}

// PaymentsFulfillStarsSubscription invokes method payments.fulfillStarsSubscription#cc5bebb3 returning error if any.
// Re-join a private channel associated to an active Telegram Star subscription »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#paid-invite-links
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/payments.fulfillStarsSubscription for reference.
func (c *Client) PaymentsFulfillStarsSubscription(ctx context.Context, request *PaymentsFulfillStarsSubscriptionRequest) (bool, error) {
	var result BoolBox

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