// Package svix this file is @generated DO NOT EDIT
package svix

import (
	"context"

	"github.com/svix/svix-webhooks/go/internal"
	"github.com/svix/svix-webhooks/go/models"
)

type StreamingStream struct {
	client *internal.SvixHttpClient
}

func newStreamingStream(client *internal.SvixHttpClient) StreamingStream {
	return StreamingStream{client}
}

type StreamingStreamListOptions struct {
	// Limit the number of returned items
	Limit *uint64
	// The iterator returned from a prior invocation
	Iterator *string

	// The sorting order of the returned items
	Order *models.Ordering
}

type StreamingStreamCreateOptions struct {
	IdempotencyKey *string
}

// List of all the organization's streams.
func (streamingStream StreamingStream) List(
	ctx context.Context,
	o *StreamingStreamListOptions,
) (*models.ListResponseStreamOut, error) {
	var err error
	queryMap := map[string]string{}
	if o == nil {
		opts := StreamingStreamListOptions{}
		o = &opts
	}
	internal.SerializeParamToMap("limit", o.Limit, queryMap, &err)
	internal.SerializeParamToMap("iterator", o.Iterator, queryMap, &err)
	internal.SerializeParamToMap("order", o.Order, queryMap, &err)
	if err != nil {
		return nil, err
	}
	return internal.ExecuteRequest[any, models.ListResponseStreamOut](
		ctx,
		streamingStream.client,
		"GET",
		"/api/v1/stream",
		nil,
		queryMap,
		nil,
		nil,
	)
}

// Creates a new stream.
func (streamingStream StreamingStream) Create(
	ctx context.Context,
	streamIn models.StreamIn,
	o *StreamingStreamCreateOptions,
) (*models.StreamOut, error) {
	var err error
	headerMap := map[string]string{}
	if o == nil {
		opts := StreamingStreamCreateOptions{}
		o = &opts
	}
	internal.SerializeParamToMap("idempotency-key", o.IdempotencyKey, headerMap, &err)
	if err != nil {
		return nil, err
	}
	return internal.ExecuteRequest[models.StreamIn, models.StreamOut](
		ctx,
		streamingStream.client,
		"POST",
		"/api/v1/stream",
		nil,
		nil,
		headerMap,
		&streamIn,
	)
}

// Get a stream by id or uid.
func (streamingStream StreamingStream) Get(
	ctx context.Context,
	streamId string,
) (*models.StreamOut, error) {
	pathMap := map[string]string{
		"stream_id": streamId,
	}
	return internal.ExecuteRequest[any, models.StreamOut](
		ctx,
		streamingStream.client,
		"GET",
		"/api/v1/stream/{stream_id}",
		pathMap,
		nil,
		nil,
		nil,
	)
}

// Create or update a stream.
func (streamingStream StreamingStream) Upsert(
	ctx context.Context,
	streamId string,
	streamIn models.StreamIn,
) (*models.StreamOut, error) {
	pathMap := map[string]string{
		"stream_id": streamId,
	}
	return internal.ExecuteRequest[models.StreamIn, models.StreamOut](
		ctx,
		streamingStream.client,
		"PUT",
		"/api/v1/stream/{stream_id}",
		pathMap,
		nil,
		nil,
		&streamIn,
	)
}

// Delete a stream.
func (streamingStream StreamingStream) Delete(
	ctx context.Context,
	streamId string,
) error {
	var err error
	pathMap := map[string]string{
		"stream_id": streamId,
	}
	_, err = internal.ExecuteRequest[any, any](
		ctx,
		streamingStream.client,
		"DELETE",
		"/api/v1/stream/{stream_id}",
		pathMap,
		nil,
		nil,
		nil,
	)
	return err
}

// Partially update a stream.
func (streamingStream StreamingStream) Patch(
	ctx context.Context,
	streamId string,
	streamPatch models.StreamPatch,
) (*models.StreamOut, error) {
	pathMap := map[string]string{
		"stream_id": streamId,
	}
	return internal.ExecuteRequest[models.StreamPatch, models.StreamOut](
		ctx,
		streamingStream.client,
		"PATCH",
		"/api/v1/stream/{stream_id}",
		pathMap,
		nil,
		nil,
		&streamPatch,
	)
}
