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

// AuthSignInRequest represents TL type `auth.signIn#8d52a951`.
// Signs in a user with a validated phone number.
//
// See https://core.telegram.org/method/auth.signIn for reference.
type AuthSignInRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Phone number in the international format
	PhoneNumber string
	// SMS-message ID, obtained from auth.sendCode¹
	//
	// Links:
	//  1) https://core.telegram.org/method/auth.sendCode
	PhoneCodeHash string
	// Valid numerical code from the SMS-message
	//
	// Use SetPhoneCode and GetPhoneCode helpers.
	PhoneCode string
	// Email verification code or token
	//
	// Use SetEmailVerification and GetEmailVerification helpers.
	EmailVerification EmailVerificationClass
}

// AuthSignInRequestTypeID is TL type id of AuthSignInRequest.
const AuthSignInRequestTypeID = 0x8d52a951

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

func (s *AuthSignInRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.PhoneNumber == "") {
		return false
	}
	if !(s.PhoneCodeHash == "") {
		return false
	}
	if !(s.PhoneCode == "") {
		return false
	}
	if !(s.EmailVerification == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AuthSignInRequest from given interface.
func (s *AuthSignInRequest) FillFrom(from interface {
	GetPhoneNumber() (value string)
	GetPhoneCodeHash() (value string)
	GetPhoneCode() (value string, ok bool)
	GetEmailVerification() (value EmailVerificationClass, ok bool)
}) {
	s.PhoneNumber = from.GetPhoneNumber()
	s.PhoneCodeHash = from.GetPhoneCodeHash()
	if val, ok := from.GetPhoneCode(); ok {
		s.PhoneCode = val
	}

	if val, ok := from.GetEmailVerification(); ok {
		s.EmailVerification = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*AuthSignInRequest) TypeName() string {
	return "auth.signIn"
}

// TypeInfo returns info about TL type.
func (s *AuthSignInRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "auth.signIn",
		ID:   AuthSignInRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PhoneNumber",
			SchemaName: "phone_number",
		},
		{
			Name:       "PhoneCodeHash",
			SchemaName: "phone_code_hash",
		},
		{
			Name:       "PhoneCode",
			SchemaName: "phone_code",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "EmailVerification",
			SchemaName: "email_verification",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *AuthSignInRequest) SetFlags() {
	if !(s.PhoneCode == "") {
		s.Flags.Set(0)
	}
	if !(s.EmailVerification == nil) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *AuthSignInRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode auth.signIn#8d52a951 as nil")
	}
	b.PutID(AuthSignInRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *AuthSignInRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode auth.signIn#8d52a951 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode auth.signIn#8d52a951: field flags: %w", err)
	}
	b.PutString(s.PhoneNumber)
	b.PutString(s.PhoneCodeHash)
	if s.Flags.Has(0) {
		b.PutString(s.PhoneCode)
	}
	if s.Flags.Has(1) {
		if s.EmailVerification == nil {
			return fmt.Errorf("unable to encode auth.signIn#8d52a951: field email_verification is nil")
		}
		if err := s.EmailVerification.Encode(b); err != nil {
			return fmt.Errorf("unable to encode auth.signIn#8d52a951: field email_verification: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *AuthSignInRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode auth.signIn#8d52a951 to nil")
	}
	if err := b.ConsumeID(AuthSignInRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode auth.signIn#8d52a951: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *AuthSignInRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode auth.signIn#8d52a951 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode auth.signIn#8d52a951: field flags: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode auth.signIn#8d52a951: field phone_number: %w", err)
		}
		s.PhoneNumber = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode auth.signIn#8d52a951: field phone_code_hash: %w", err)
		}
		s.PhoneCodeHash = value
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode auth.signIn#8d52a951: field phone_code: %w", err)
		}
		s.PhoneCode = value
	}
	if s.Flags.Has(1) {
		value, err := DecodeEmailVerification(b)
		if err != nil {
			return fmt.Errorf("unable to decode auth.signIn#8d52a951: field email_verification: %w", err)
		}
		s.EmailVerification = value
	}
	return nil
}

// GetPhoneNumber returns value of PhoneNumber field.
func (s *AuthSignInRequest) GetPhoneNumber() (value string) {
	if s == nil {
		return
	}
	return s.PhoneNumber
}

// GetPhoneCodeHash returns value of PhoneCodeHash field.
func (s *AuthSignInRequest) GetPhoneCodeHash() (value string) {
	if s == nil {
		return
	}
	return s.PhoneCodeHash
}

// SetPhoneCode sets value of PhoneCode conditional field.
func (s *AuthSignInRequest) SetPhoneCode(value string) {
	s.Flags.Set(0)
	s.PhoneCode = value
}

// GetPhoneCode returns value of PhoneCode conditional field and
// boolean which is true if field was set.
func (s *AuthSignInRequest) GetPhoneCode() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.PhoneCode, true
}

// SetEmailVerification sets value of EmailVerification conditional field.
func (s *AuthSignInRequest) SetEmailVerification(value EmailVerificationClass) {
	s.Flags.Set(1)
	s.EmailVerification = value
}

// GetEmailVerification returns value of EmailVerification conditional field and
// boolean which is true if field was set.
func (s *AuthSignInRequest) GetEmailVerification() (value EmailVerificationClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.EmailVerification, true
}

// AuthSignIn invokes method auth.signIn#8d52a951 returning error if any.
// Signs in a user with a validated phone number.
//
// Possible errors:
//
//	500 AUTH_RESTART: Restart the authorization process.
//	400 PHONE_CODE_EMPTY: phone_code is missing.
//	400 PHONE_CODE_EXPIRED: The phone code you provided has expired.
//	400 PHONE_CODE_INVALID: The provided phone code is invalid.
//	406 PHONE_NUMBER_INVALID: The phone number is invalid.
//	400 PHONE_NUMBER_UNOCCUPIED: The phone number is not yet being used.
//	500 SIGN_IN_FAILED: Failure while signing in.
//	406 UPDATE_APP_TO_LOGIN: Please update your client to login.
//
// See https://core.telegram.org/method/auth.signIn for reference.
func (c *Client) AuthSignIn(ctx context.Context, request *AuthSignInRequest) (AuthAuthorizationClass, error) {
	var result AuthAuthorizationBox

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