// Code generated by Fern. DO NOT EDIT.

package identities

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"
)

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,
			},
		),
	}
}

// Link two user accounts together forming a primary and secondary relationship. On successful linking, the endpoint returns the new array of the primary account identities.
//
// Note: There are two ways of invoking the endpoint:
//
// <ul>
//
//	<li>With the authenticated primary account's JWT in the Authorization header, which has the <code>update:current_user_identities</code> scope:
//	  <pre>
//	    POST /api/v2/users/PRIMARY_ACCOUNT_USER_ID/identities
//	    Authorization: "Bearer PRIMARY_ACCOUNT_JWT"
//	    {
//	      "link_with": "SECONDARY_ACCOUNT_JWT"
//	    }
//	  </pre>
//	  In this case, only the <code>link_with</code> param is required in the body, which also contains the JWT obtained upon the secondary account's authentication.
//	</li>
//	<li>With a token generated by the API V2 containing the <code>update:users</code> scope:
//	  <pre>
//	  POST /api/v2/users/PRIMARY_ACCOUNT_USER_ID/identities
//	  Authorization: "Bearer YOUR_API_V2_TOKEN"
//	  {
//	    "provider": "SECONDARY_ACCOUNT_PROVIDER",
//	    "connection_id": "SECONDARY_ACCOUNT_CONNECTION_ID(OPTIONAL)",
//	    "user_id": "SECONDARY_ACCOUNT_USER_ID"
//	  }
//	  </pre>
//	  In this case you need to send <code>provider</code> and <code>user_id</code> in the body. Optionally you can also send the <code>connection_id</code> param which is suitable for identifying a particular database connection for the 'auth0' provider.
//	</li>
//
// </ul>
func (c *Client) Link(
	ctx context.Context,
	// ID of the primary user account to link a second user account to.
	id string,
	request *management.LinkUserIdentityRequestContent,
	opts ...option.RequestOption,
) ([]*management.UserIdentity, error) {
	response, err := c.WithRawResponse.Link(
		ctx,
		id,
		request,
		opts...,
	)
	if err != nil {
		return nil, err
	}
	return response.Body, nil
}

// Unlink a specific secondary account from a target user. This action requires the ID of both the target user and the secondary account.
//
// Unlinking the secondary account removes it from the identities array of the target user and creates a new standalone profile for the secondary account. To learn more, review <a href="https://auth0.com/docs/manage-users/user-accounts/user-account-linking/unlink-user-accounts">Unlink User Accounts</a>.
func (c *Client) Delete(
	ctx context.Context,
	// ID of the primary user account.
	id string,
	// Identity provider name of the secondary linked account (e.g. `google-oauth2`).
	provider *management.UserIdentityProviderEnum,
	// ID of the secondary linked account (e.g. `123456789081523216417` part after the `|` in `google-oauth2|123456789081523216417`).
	userID string,
	opts ...option.RequestOption,
) (management.DeleteUserIdentityResponseContent, error) {
	response, err := c.WithRawResponse.Delete(
		ctx,
		id,
		provider,
		userID,
		opts...,
	)
	if err != nil {
		return nil, err
	}
	return response.Body, nil
}
