// Code generated by Fern. DO NOT EDIT.

package client

import (
	context "context"
	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"
	roles "github.com/auth0/go-auth0/v2/management/organizations/members/roles"
	http "net/http"
)

type Client struct {
	WithRawResponse *RawClient
	Roles           *roles.Client

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

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

// List organization members.
// This endpoint is subject to eventual consistency. New users may not be immediately included in the response and deleted users may not be immediately removed from it.
//
// <ul>
//
//	<li>
//	  Use the <code>fields</code> parameter to optionally define the specific member details retrieved. If <code>fields</code> is left blank, all fields (except roles) are returned.
//	</li>
//	<li>
//	  Member roles are not sent by default. Use <code>fields=roles</code> to retrieve the roles assigned to each listed member. To use this parameter, you must include the <code>read:organization_member_roles</code> scope in the token.
//	</li>
//
// </ul>
//
// This endpoint supports two types of pagination:
//
// - Offset pagination
// - Checkpoint pagination
//
// Checkpoint pagination must be used if you need to retrieve more than 1000 organization members.
//
// <h2>Checkpoint Pagination</h2>
//
// To search by checkpoint, use the following parameters: - from: Optional id from which to start selection. - take: The total amount of entries to retrieve when using the from parameter. Defaults to 50. Note: The first time you call this endpoint using Checkpoint Pagination, you should omit the <code>from</code> parameter. If there are more results, a <code>next</code> value will be included in the response. You can use this for subsequent API calls. When <code>next</code> is no longer included in the response, this indicates there are no more pages remaining.
func (c *Client) List(
	ctx context.Context,
	// Organization identifier.
	id string,
	request *management.ListOrganizationMembersRequestParameters,
	opts ...option.RequestOption,
) (*core.Page[*management.OrganizationMember], error) {
	options := core.NewRequestOptions(opts...)
	baseURL := internal.ResolveBaseURL(
		options.BaseURL,
		c.baseURL,
		"https://%7BTENANT%7D.auth0.com/api/v2",
	)
	endpointURL := internal.EncodeURL(
		baseURL+"/organizations/%v/members",
		id,
	)
	queryParams, err := internal.QueryValues(request)
	if err != nil {
		return nil, err
	}
	headers := internal.MergeHeaders(
		c.options.ToHeader(),
		options.ToHeader(),
	)
	prepareCall := func(pageRequest *internal.PageRequest[*string]) *internal.CallParams {
		if pageRequest.Cursor != nil {
			queryParams.Set("from", *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),
		}
	}
	readPageResponse := func(response *management.ListOrganizationMembersPaginatedResponseContent) *internal.PageResponse[*string, *management.OrganizationMember] {
		var zeroValue *string
		next := response.Next
		results := response.Members
		return &internal.PageResponse[*string, *management.OrganizationMember]{
			Next:    next,
			Results: results,
			Done:    next == zeroValue,
		}
	}
	pager := internal.NewCursorPager(
		c.caller,
		prepareCall,
		readPageResponse,
	)
	return pager.GetPage(ctx, request.From)
}

// Set one or more existing users as members of a specific <a href="https://auth0.com/docs/manage-users/organizations">Organization</a>.
//
// To add a user to an Organization through this action, the user must already exist in your tenant. If a user does not yet exist, you can <a href="https://auth0.com/docs/manage-users/organizations/configure-organizations/invite-members">invite them to create an account</a>, manually create them through the Auth0 Dashboard, or use the Management API.
func (c *Client) Create(
	ctx context.Context,
	// Organization identifier.
	id string,
	request *management.CreateOrganizationMemberRequestContent,
	opts ...option.RequestOption,
) error {
	_, err := c.WithRawResponse.Create(
		ctx,
		id,
		request,
		opts...,
	)
	if err != nil {
		return err
	}
	return nil
}

func (c *Client) Delete(
	ctx context.Context,
	// Organization identifier.
	id string,
	request *management.DeleteOrganizationMembersRequestContent,
	opts ...option.RequestOption,
) error {
	_, err := c.WithRawResponse.Delete(
		ctx,
		id,
		request,
		opts...,
	)
	if err != nil {
		return err
	}
	return nil
}
