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

// StoriesGetPeerStoriesRequest represents TL type `stories.getPeerStories#2c4ada50`.
// Fetch the full active story list¹ of a specific peer.
//
// Links:
//  1. https://core.telegram.org/api/stories#watching-stories
//
// See https://core.telegram.org/method/stories.getPeerStories for reference.
type StoriesGetPeerStoriesRequest struct {
	// Peer whose stories should be fetched
	Peer InputPeerClass
}

// StoriesGetPeerStoriesRequestTypeID is TL type id of StoriesGetPeerStoriesRequest.
const StoriesGetPeerStoriesRequestTypeID = 0x2c4ada50

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

func (g *StoriesGetPeerStoriesRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Peer == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesGetPeerStoriesRequest from given interface.
func (g *StoriesGetPeerStoriesRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
}) {
	g.Peer = from.GetPeer()
}

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

// TypeName returns name of type in TL schema.
func (*StoriesGetPeerStoriesRequest) TypeName() string {
	return "stories.getPeerStories"
}

// TypeInfo returns info about TL type.
func (g *StoriesGetPeerStoriesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.getPeerStories",
		ID:   StoriesGetPeerStoriesRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *StoriesGetPeerStoriesRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getPeerStories#2c4ada50 as nil")
	}
	b.PutID(StoriesGetPeerStoriesRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *StoriesGetPeerStoriesRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getPeerStories#2c4ada50 as nil")
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode stories.getPeerStories#2c4ada50: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.getPeerStories#2c4ada50: field peer: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *StoriesGetPeerStoriesRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getPeerStories#2c4ada50 to nil")
	}
	if err := b.ConsumeID(StoriesGetPeerStoriesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.getPeerStories#2c4ada50: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *StoriesGetPeerStoriesRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getPeerStories#2c4ada50 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.getPeerStories#2c4ada50: field peer: %w", err)
		}
		g.Peer = value
	}
	return nil
}

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

// StoriesGetPeerStories invokes method stories.getPeerStories#2c4ada50 returning error if any.
// Fetch the full active story list¹ of a specific peer.
//
// Links:
//  1. https://core.telegram.org/api/stories#watching-stories
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//	400 MSG_ID_INVALID: Invalid message ID provided.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/stories.getPeerStories for reference.
func (c *Client) StoriesGetPeerStories(ctx context.Context, peer InputPeerClass) (*StoriesPeerStories, error) {
	var result StoriesPeerStories

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