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

// AccountDeleteAccountRequest represents TL type `account.deleteAccount#a2c0cf74`.
// Delete the user's account from the telegram servers.
// Can also be used to delete the account of a user that provided the login code, but
// forgot the 2FA password and no recovery method is configured, see here »¹ for more
// info on password recovery, and here »² for more info on account deletion.
//
// Links:
//  1. https://core.telegram.org/api/srp#password-recovery
//  2. https://core.telegram.org/api/account-deletion
//
// See https://core.telegram.org/method/account.deleteAccount for reference.
type AccountDeleteAccountRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Why is the account being deleted, can be empty
	Reason string
	// 2FA password¹: this field can be omitted even for accounts with 2FA enabled: in this
	// case account account deletion will be delayed by 7 days as specified in the docs »²
	//
	// Links:
	//  1) https://core.telegram.org/api/srp
	//  2) https://core.telegram.org/api/account-deletion
	//
	// Use SetPassword and GetPassword helpers.
	Password InputCheckPasswordSRPClass
}

// AccountDeleteAccountRequestTypeID is TL type id of AccountDeleteAccountRequest.
const AccountDeleteAccountRequestTypeID = 0xa2c0cf74

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

func (d *AccountDeleteAccountRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Flags.Zero()) {
		return false
	}
	if !(d.Reason == "") {
		return false
	}
	if !(d.Password == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountDeleteAccountRequest from given interface.
func (d *AccountDeleteAccountRequest) FillFrom(from interface {
	GetReason() (value string)
	GetPassword() (value InputCheckPasswordSRPClass, ok bool)
}) {
	d.Reason = from.GetReason()
	if val, ok := from.GetPassword(); ok {
		d.Password = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (d *AccountDeleteAccountRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.deleteAccount",
		ID:   AccountDeleteAccountRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Reason",
			SchemaName: "reason",
		},
		{
			Name:       "Password",
			SchemaName: "password",
			Null:       !d.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (d *AccountDeleteAccountRequest) SetFlags() {
	if !(d.Password == nil) {
		d.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (d *AccountDeleteAccountRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode account.deleteAccount#a2c0cf74 as nil")
	}
	b.PutID(AccountDeleteAccountRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *AccountDeleteAccountRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode account.deleteAccount#a2c0cf74 as nil")
	}
	d.SetFlags()
	if err := d.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.deleteAccount#a2c0cf74: field flags: %w", err)
	}
	b.PutString(d.Reason)
	if d.Flags.Has(0) {
		if d.Password == nil {
			return fmt.Errorf("unable to encode account.deleteAccount#a2c0cf74: field password is nil")
		}
		if err := d.Password.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.deleteAccount#a2c0cf74: field password: %w", err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (d *AccountDeleteAccountRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode account.deleteAccount#a2c0cf74 to nil")
	}
	{
		if err := d.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.deleteAccount#a2c0cf74: field flags: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode account.deleteAccount#a2c0cf74: field reason: %w", err)
		}
		d.Reason = value
	}
	if d.Flags.Has(0) {
		value, err := DecodeInputCheckPasswordSRP(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.deleteAccount#a2c0cf74: field password: %w", err)
		}
		d.Password = value
	}
	return nil
}

// GetReason returns value of Reason field.
func (d *AccountDeleteAccountRequest) GetReason() (value string) {
	if d == nil {
		return
	}
	return d.Reason
}

// SetPassword sets value of Password conditional field.
func (d *AccountDeleteAccountRequest) SetPassword(value InputCheckPasswordSRPClass) {
	d.Flags.Set(0)
	d.Password = value
}

// GetPassword returns value of Password conditional field and
// boolean which is true if field was set.
func (d *AccountDeleteAccountRequest) GetPassword() (value InputCheckPasswordSRPClass, ok bool) {
	if d == nil {
		return
	}
	if !d.Flags.Has(0) {
		return value, false
	}
	return d.Password, true
}

// GetPasswordAsNotEmpty returns mapped value of Password conditional field and
// boolean which is true if field was set.
func (d *AccountDeleteAccountRequest) GetPasswordAsNotEmpty() (*InputCheckPasswordSRP, bool) {
	if value, ok := d.GetPassword(); ok {
		return value.AsNotEmpty()
	}
	return nil, false
}

// AccountDeleteAccount invokes method account.deleteAccount#a2c0cf74 returning error if any.
// Delete the user's account from the telegram servers.
// Can also be used to delete the account of a user that provided the login code, but
// forgot the 2FA password and no recovery method is configured, see here »¹ for more
// info on password recovery, and here »² for more info on account deletion.
//
// Links:
//  1. https://core.telegram.org/api/srp#password-recovery
//  2. https://core.telegram.org/api/account-deletion
//
// Possible errors:
//
//	420 2FA_CONFIRM_WAIT_%d: Since this account is active and protected by a 2FA password, we will delete it in 1 week for security purposes. You can cancel this process at any time, you'll be able to reset your account in %d seconds.
//	400 PASSWORD_HASH_INVALID: The provided password hash is invalid.
//
// See https://core.telegram.org/method/account.deleteAccount for reference.
func (c *Client) AccountDeleteAccount(ctx context.Context, request *AccountDeleteAccountRequest) (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
}
