// Code generated by Fern. DO NOT EDIT.

package bindings

import (
	context "context"
	fmt "fmt"
	management "github.com/auth0/go-auth0/v2/management"
	core "github.com/auth0/go-auth0/v2/management/core"
	internal "github.com/auth0/go-auth0/v2/management/internal"
	option "github.com/auth0/go-auth0/v2/management/option"
	http "net/http"
	strconv "strconv"
)

type Client struct {
	WithRawResponse *RawClient

	options *core.RequestOptions
	baseURL string
	caller  *internal.Caller
}

func NewClient(options *core.RequestOptions) *Client {
	return &Client{
		WithRawResponse: NewRawClient(options),
		options:         options,
		baseURL:         options.BaseURL,
		caller: internal.NewCaller(
			&internal.CallerParams{
				Client:      options.HTTPClient,
				MaxAttempts: options.MaxAttempts,
			},
		),
	}
}

// Retrieve the actions that are bound to a trigger. Once an action is created and deployed, it must be attached (i.e. bound) to a trigger so that it will be executed as part of a flow. The list of actions returned reflects the order in which they will be executed during the appropriate flow.
func (c *Client) List(
	ctx context.Context,
	// An actions extensibility point.
	triggerID management.ActionTriggerTypeEnum,
	request *management.ListActionTriggerBindingsRequestParameters,
	opts ...option.RequestOption,
) (*core.Page[*management.ActionBinding], error) {
	options := core.NewRequestOptions(opts...)
	baseURL := internal.ResolveBaseURL(
		options.BaseURL,
		c.baseURL,
		"https://%7BTENANT%7D.auth0.com/api/v2",
	)
	endpointURL := internal.EncodeURL(
		baseURL+"/actions/triggers/%v/bindings",
		triggerID,
	)
	queryParams, err := internal.QueryValues(request)
	if err != nil {
		return nil, err
	}
	headers := internal.MergeHeaders(
		c.options.ToHeader(),
		options.ToHeader(),
	)
	prepareCall := func(pageRequest *internal.PageRequest[*int]) *internal.CallParams {
		if pageRequest.Cursor != nil {
			queryParams.Set("page", fmt.Sprintf("%v", *pageRequest.Cursor))
		}
		nextURL := endpointURL
		if len(queryParams) > 0 {
			nextURL += "?" + queryParams.Encode()
		}
		return &internal.CallParams{
			URL:             nextURL,
			Method:          http.MethodGet,
			Headers:         headers,
			MaxAttempts:     options.MaxAttempts,
			BodyProperties:  options.BodyProperties,
			QueryParameters: options.QueryParameters,
			Client:          options.HTTPClient,
			Response:        pageRequest.Response,
			ErrorDecoder:    internal.NewErrorDecoder(management.ErrorCodes),
		}
	}
	next := 1
	if queryParams.Has("page") {
		var err error
		if next, err = strconv.Atoi(queryParams.Get("page")); err != nil {
			return nil, err
		}
	}

	readPageResponse := func(response *management.ListActionBindingsPaginatedResponseContent) *internal.PageResponse[*int, *management.ActionBinding] {
		next += 1
		results := response.Bindings
		return &internal.PageResponse[*int, *management.ActionBinding]{
			Next:    &next,
			Results: results,
		}
	}
	pager := internal.NewOffsetPager(
		c.caller,
		prepareCall,
		readPageResponse,
	)
	return pager.GetPage(ctx, &next)
}

// Update the actions that are bound (i.e. attached) to a trigger. Once an action is created and deployed, it must be attached (i.e. bound) to a trigger so that it will be executed as part of a flow. The order in which the actions are provided will determine the order in which they are executed.
func (c *Client) UpdateMany(
	ctx context.Context,
	// An actions extensibility point.
	triggerID management.ActionTriggerTypeEnum,
	request *management.UpdateActionBindingsRequestContent,
	opts ...option.RequestOption,
) (*management.UpdateActionBindingsResponseContent, error) {
	response, err := c.WithRawResponse.UpdateMany(
		ctx,
		triggerID,
		request,
		opts...,
	)
	if err != nil {
		return nil, err
	}
	return response.Body, nil
}
