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

// BotsUpdateStarRefProgramRequest represents TL type `bots.updateStarRefProgram#778b5ab3`.
// Create, edit or delete the affiliate program¹ of a bot we own
//
// Links:
//  1. https://core.telegram.org/api/bots/referrals
//
// See https://core.telegram.org/method/bots.updateStarRefProgram for reference.
type BotsUpdateStarRefProgramRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The bot
	Bot InputUserClass
	// The permille commission rate: it indicates the share of Telegram Stars received by
	// affiliates for every transaction made by users they referred inside of the bot.    The
	// minimum and maximum values for this parameter are contained in the
	// starref_min_commission_permille¹ and starref_max_commission_permille² client
	// configuration parameters.   Can be 0 to terminate the affiliate program.  Both the
	// duration and the commission may only be raised after creation of the program: to lower
	// them, the program must first be terminated and a new one created.
	//
	// Links:
	//  1) https://core.telegram.org/api/config#starref-min-commission-permille
	//  2) https://core.telegram.org/api/config#starref-max-commission-permille
	CommissionPermille int
	// Indicates the duration of the affiliate program; if not set, there is no expiration
	// date.
	//
	// Use SetDurationMonths and GetDurationMonths helpers.
	DurationMonths int
}

// BotsUpdateStarRefProgramRequestTypeID is TL type id of BotsUpdateStarRefProgramRequest.
const BotsUpdateStarRefProgramRequestTypeID = 0x778b5ab3

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

func (u *BotsUpdateStarRefProgramRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.Bot == nil) {
		return false
	}
	if !(u.CommissionPermille == 0) {
		return false
	}
	if !(u.DurationMonths == 0) {
		return false
	}

	return true
}

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

// FillFrom fills BotsUpdateStarRefProgramRequest from given interface.
func (u *BotsUpdateStarRefProgramRequest) FillFrom(from interface {
	GetBot() (value InputUserClass)
	GetCommissionPermille() (value int)
	GetDurationMonths() (value int, ok bool)
}) {
	u.Bot = from.GetBot()
	u.CommissionPermille = from.GetCommissionPermille()
	if val, ok := from.GetDurationMonths(); ok {
		u.DurationMonths = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*BotsUpdateStarRefProgramRequest) TypeName() string {
	return "bots.updateStarRefProgram"
}

// TypeInfo returns info about TL type.
func (u *BotsUpdateStarRefProgramRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.updateStarRefProgram",
		ID:   BotsUpdateStarRefProgramRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "CommissionPermille",
			SchemaName: "commission_permille",
		},
		{
			Name:       "DurationMonths",
			SchemaName: "duration_months",
			Null:       !u.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *BotsUpdateStarRefProgramRequest) SetFlags() {
	if !(u.DurationMonths == 0) {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *BotsUpdateStarRefProgramRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode bots.updateStarRefProgram#778b5ab3 as nil")
	}
	b.PutID(BotsUpdateStarRefProgramRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *BotsUpdateStarRefProgramRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode bots.updateStarRefProgram#778b5ab3 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.updateStarRefProgram#778b5ab3: field flags: %w", err)
	}
	if u.Bot == nil {
		return fmt.Errorf("unable to encode bots.updateStarRefProgram#778b5ab3: field bot is nil")
	}
	if err := u.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.updateStarRefProgram#778b5ab3: field bot: %w", err)
	}
	b.PutInt(u.CommissionPermille)
	if u.Flags.Has(0) {
		b.PutInt(u.DurationMonths)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *BotsUpdateStarRefProgramRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode bots.updateStarRefProgram#778b5ab3 to nil")
	}
	if err := b.ConsumeID(BotsUpdateStarRefProgramRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.updateStarRefProgram#778b5ab3: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *BotsUpdateStarRefProgramRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode bots.updateStarRefProgram#778b5ab3 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode bots.updateStarRefProgram#778b5ab3: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.updateStarRefProgram#778b5ab3: field bot: %w", err)
		}
		u.Bot = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode bots.updateStarRefProgram#778b5ab3: field commission_permille: %w", err)
		}
		u.CommissionPermille = value
	}
	if u.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode bots.updateStarRefProgram#778b5ab3: field duration_months: %w", err)
		}
		u.DurationMonths = value
	}
	return nil
}

// GetBot returns value of Bot field.
func (u *BotsUpdateStarRefProgramRequest) GetBot() (value InputUserClass) {
	if u == nil {
		return
	}
	return u.Bot
}

// GetCommissionPermille returns value of CommissionPermille field.
func (u *BotsUpdateStarRefProgramRequest) GetCommissionPermille() (value int) {
	if u == nil {
		return
	}
	return u.CommissionPermille
}

// SetDurationMonths sets value of DurationMonths conditional field.
func (u *BotsUpdateStarRefProgramRequest) SetDurationMonths(value int) {
	u.Flags.Set(0)
	u.DurationMonths = value
}

// GetDurationMonths returns value of DurationMonths conditional field and
// boolean which is true if field was set.
func (u *BotsUpdateStarRefProgramRequest) GetDurationMonths() (value int, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(0) {
		return value, false
	}
	return u.DurationMonths, true
}

// BotsUpdateStarRefProgram invokes method bots.updateStarRefProgram#778b5ab3 returning error if any.
// Create, edit or delete the affiliate program¹ of a bot we own
//
// Links:
//  1. https://core.telegram.org/api/bots/referrals
//
// Possible errors:
//
//	400 BOT_INVALID: This is not a valid bot.
//	400 STARREF_AWAITING_END: The previous referral program was terminated less than 24 hours ago: further changes can be made after the date specified in userFull.starref_program.end_date.
//	400 STARREF_PERMILLE_INVALID: The specified commission_permille is invalid: the minimum and maximum values for this parameter are contained in the starref_min_commission_permille and starref_max_commission_permille client configuration parameters.
//	400 STARREF_PERMILLE_TOO_LOW: The specified commission_permille is too low: the minimum and maximum values for this parameter are contained in the starref_min_commission_permille and starref_max_commission_permille client configuration parameters.
//
// See https://core.telegram.org/method/bots.updateStarRefProgram for reference.
func (c *Client) BotsUpdateStarRefProgram(ctx context.Context, request *BotsUpdateStarRefProgramRequest) (*StarRefProgram, error) {
	var result StarRefProgram

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