// Code generated by oagen. DO NOT EDIT.

package workos

import (
	
	
	
	
)

// OrganizationMembershipService handles OrganizationMembership operations.
type OrganizationMembershipService struct {
	client *Client
}

// OrganizationMembershipRole is one of:
//   - OrganizationMembershipRoleSingle
//   - OrganizationMembershipRoleMultiple
type OrganizationMembershipRole interface {
	isOrganizationMembershipRole()
	applyToBody(map[string]any)
}

type OrganizationMembershipRoleSingle struct {
	Slug string
}

func ( OrganizationMembershipRoleSingle) () {}
func ( OrganizationMembershipRoleSingle) ( map[string]any) {
	["role_slug"] = .Slug
}

type OrganizationMembershipRoleMultiple struct {
	Slugs []string
}

func ( OrganizationMembershipRoleMultiple) () {}
func ( OrganizationMembershipRoleMultiple) ( map[string]any) {
	["role_slugs"] = .Slugs
}

// OrganizationMembershipListParams contains the parameters for List.
type OrganizationMembershipListParams struct {
	PaginationParams
	// OrganizationID is the ID of the [organization](https://workos.com/docs/reference/organization) which the user belongs to.
	OrganizationID *string `url:"organization_id,omitempty" json:"-"`
	// Statuses is filter by the status of the organization membership. Array including any of `active`, `inactive`, or `pending`.
	Statuses []UserManagementOrganizationMembershipStatuses `url:"statuses,omitempty" json:"-"`
	// UserID is the ID of the [user](https://workos.com/docs/reference/authkit/user).
	UserID *string `url:"user_id,omitempty" json:"-"`
}

// List organization memberships
// Get a list of all organization memberships matching the criteria specified. At least one of `user_id` or `organization_id` must be provided. By default only active memberships are returned. Use the `statuses` parameter to filter by other statuses.
func ( *OrganizationMembershipService) ( context.Context,  *OrganizationMembershipListParams,  ...RequestOption) *Iterator[UserOrganizationMembership] {
	return newIterator[UserOrganizationMembership](, .client, "GET", "/user_management/organization_memberships", , "after", "data", , map[string]string{"limit": "10", "order": "desc"})
}

// OrganizationMembershipCreateParams contains the parameters for Create.
type OrganizationMembershipCreateParams struct {
	// UserID is the ID of the [user](https://workos.com/docs/reference/authkit/user).
	UserID string `json:"user_id" url:"-"`
	// OrganizationID is the ID of the [organization](https://workos.com/docs/reference/organization) which the user belongs to.
	OrganizationID string `json:"organization_id" url:"-"`
	// Role optionally identifies the role.
	Role OrganizationMembershipRole `url:"-" json:"-"`
}

// MarshalJSON implements json.Marshaler for OrganizationMembershipCreateParams.
func ( OrganizationMembershipCreateParams) () ([]byte, error) {
	type  OrganizationMembershipCreateParams
	,  := json.Marshal(())
	if  != nil {
		return nil, 
	}
	if .Role == nil {
		return , nil
	}
	var  map[string]any
	if  := json.Unmarshal(, &);  != nil {
		return nil, 
	}
	if .Role != nil {
		.Role.applyToBody()
	}
	return json.Marshal()
}

// Create an organization membership
// Creates a new `active` organization membership for the given organization and user.
// Calling this API with an organization and user that match an `inactive` organization membership will activate the membership with the specified role(s).
func ( *OrganizationMembershipService) ( context.Context,  *OrganizationMembershipCreateParams,  ...RequestOption) (*OrganizationMembership, error) {
	var  OrganizationMembership
	,  := .client.request(, "POST", "/user_management/organization_memberships", nil, , &, )
	if  != nil {
		return nil, 
	}
	return &, nil
}

// Get an organization membership
// Get the details of an existing organization membership.
func ( *OrganizationMembershipService) ( context.Context,  string,  ...RequestOption) (*UserOrganizationMembership, error) {
	var  UserOrganizationMembership
	,  := .client.request(, "GET", fmt.Sprintf("/user_management/organization_memberships/%s", url.PathEscape()), nil, nil, &, )
	if  != nil {
		return nil, 
	}
	return &, nil
}

// OrganizationMembershipUpdateParams contains the parameters for Update.
type OrganizationMembershipUpdateParams struct {
	// Role optionally identifies the role.
	Role OrganizationMembershipRole `url:"-" json:"-"`
}

// MarshalJSON implements json.Marshaler for OrganizationMembershipUpdateParams.
func ( OrganizationMembershipUpdateParams) () ([]byte, error) {
	type  OrganizationMembershipUpdateParams
	,  := json.Marshal(())
	if  != nil {
		return nil, 
	}
	if .Role == nil {
		return , nil
	}
	var  map[string]any
	if  := json.Unmarshal(, &);  != nil {
		return nil, 
	}
	if .Role != nil {
		.Role.applyToBody()
	}
	return json.Marshal()
}

// Update an organization membership
// Update the details of an existing organization membership.
func ( *OrganizationMembershipService) ( context.Context,  string,  *OrganizationMembershipUpdateParams,  ...RequestOption) (*UserOrganizationMembership, error) {
	var  UserOrganizationMembership
	,  := .client.request(, "PUT", fmt.Sprintf("/user_management/organization_memberships/%s", url.PathEscape()), nil, , &, )
	if  != nil {
		return nil, 
	}
	return &, nil
}

// Delete an organization membership
// Permanently deletes an existing organization membership. It cannot be undone.
func ( *OrganizationMembershipService) ( context.Context,  string,  ...RequestOption) error {
	,  := .client.request(, "DELETE", fmt.Sprintf("/user_management/organization_memberships/%s", url.PathEscape()), nil, nil, nil, )
	return 
}

// Deactivate an organization membership
// Deactivates an `active` organization membership. Emits an [organization_membership.updated](https://workos.com/docs/events/organization-membership) event upon successful deactivation.
// - Deactivating an `inactive` membership is a no-op and does not emit an event.
// - Deactivating a `pending` membership returns an error. This membership should be [deleted](https://workos.com/docs/reference/authkit/organization-membership/delete) instead.
// See the [membership management documentation](https://workos.com/docs/authkit/users-organizations/organizations/membership-management) for additional details.
func ( *OrganizationMembershipService) ( context.Context,  string,  ...RequestOption) (*OrganizationMembership, error) {
	var  OrganizationMembership
	,  := .client.request(, "PUT", fmt.Sprintf("/user_management/organization_memberships/%s/deactivate", url.PathEscape()), nil, nil, &, )
	if  != nil {
		return nil, 
	}
	return &, nil
}

// Reactivate an organization membership
// Reactivates an `inactive` organization membership, retaining the pre-existing role(s). Emits an [organization_membership.updated](https://workos.com/docs/events/organization-membership) event upon successful reactivation.
// - Reactivating an `active` membership is a no-op and does not emit an event.
// - Reactivating a `pending` membership returns an error. The user needs to [accept the invitation](https://workos.com/docs/authkit/invitations) instead.
// See the [membership management documentation](https://workos.com/docs/authkit/users-organizations/organizations/membership-management) for additional details.
func ( *OrganizationMembershipService) ( context.Context,  string,  ...RequestOption) (*UserOrganizationMembership, error) {
	var  UserOrganizationMembership
	,  := .client.request(, "PUT", fmt.Sprintf("/user_management/organization_memberships/%s/reactivate", url.PathEscape()), nil, nil, &, )
	if  != nil {
		return nil, 
	}
	return &, nil
}

// OrganizationMembershipListGroupsParams contains the parameters for ListGroups.
type OrganizationMembershipListGroupsParams struct {
	PaginationParams
}

// ListGroups
// Get a list of groups that an organization membership belongs to.
func ( *OrganizationMembershipService) ( context.Context,  string,  *OrganizationMembershipListGroupsParams,  ...RequestOption) *Iterator[Group] {
	return newIterator[Group](, .client, "GET", fmt.Sprintf("/user_management/organization_memberships/%s/groups", url.PathEscape()), , "after", "data", , map[string]string{"limit": "10", "order": "desc"})
}