class OpenApiSDK::SpaireMembers

def add_member(request:, timeout_ms: nil, http_headers: nil)

def add_member(request:, timeout_ms: nil, http_headers: nil)
  # add_member - Add Member
  # Add a new member to the customer's team.
  #
  # Only available to owners and billing managers of team customers.
  #
  # Rules:
  # - Cannot add a member with the owner role (there must be exactly one owner)
  # - If a member with this email already exists, the existing member is returned
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = "#{base_url}/v1/customer-portal/members"
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  req_content_type, data, form = Utils.serialize_request_body(request, false, false, :request, :json)
  headers["content-type"] = req_content_type
  raise StandardError, "request body is required" if data.nil? && form.nil?
  if form && !form.empty?
    body = Utils.encode_form(form)
  elsif Utils.match_content_type(req_content_type, "application/x-www-form-urlencoded")
    body = URI.encode_www_form(T.cast(data, T::Hash[Symbol, Object]))
  else
    body = data
  end
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "customer_portal:members:add_member",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).post(url) do |req|
      req.body = body
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["201"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Components::CustomerPortalMember)
      response = Models::Operations::CustomerPortalMembersAddMemberResponse.new(
        status_code: http_response.status,
        content_type: content_type,
        raw_response: http_response,
        customer_portal_member: T.unsafe(obj)
      )
      return response
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["400", "401", "403", "4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end

def get_url(base_url:, url_variables: nil)

def get_url(base_url:, url_variables: nil)
  sd_base_url, sd_options = @sdk_configuration.get_server_details
  if base_url.nil?
    base_url = sd_base_url
  end
  if url_variables.nil?
    url_variables = sd_options
  end
  return Utils.template_url(base_url, url_variables)
end

def initialize(sdk_config)

def initialize(sdk_config)
  @sdk_configuration = sdk_config
end

def list_members(timeout_ms: nil, http_headers: nil)

def list_members(timeout_ms: nil, http_headers: nil)
  # list_members - List Members
  # List all members of the customer's team.
  #
  # Only available to owners and billing managers of team customers.
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = "#{base_url}/v1/customer-portal/members"
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "customer_portal:members:list_members",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).get(url) do |req|
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["200"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(
        JSON.parse(response_data),
        Crystalline::Array.new(Models::Components::CustomerPortalMember)
      )
      response = Models::Operations::CustomerPortalMembersListMembersResponse.new(
        status_code: http_response.status,
        content_type: content_type,
        raw_response: http_response,
        response_customer_portal_members_list_members: T.unsafe(obj)
      )
      return response
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["401", "403", "4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end

def remove_member(id:, timeout_ms: nil, http_headers: nil)

def remove_member(id:, timeout_ms: nil, http_headers: nil)
  # remove_member - Remove Member
  # Remove a member from the team.
  #
  # Only available to owners and billing managers of team customers.
  #
  # Rules:
  # - Cannot remove yourself
  # - Cannot remove the only owner
  request = Models::Operations::CustomerPortalMembersRemoveMemberRequest.new(
    id: id
  )
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = Utils.generate_url(
    Models::Operations::CustomerPortalMembersRemoveMemberRequest,
    base_url,
    "/v1/customer-portal/members/{id}",
    request
  )
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "customer_portal:members:remove_member",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).delete(url) do |req|
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["204"])
    http_response = @sdk_configuration.hooks.after_success(
      hook_ctx: SDKHooks::AfterSuccessHookContext.new(
        hook_ctx: hook_ctx
      ),
      response: http_response
    )
    return Models::Operations::CustomerPortalMembersRemoveMemberResponse.new(
      status_code: http_response.status,
      content_type: content_type,
      raw_response: http_response
    )
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["400", "401", "403", "404", "4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end

def update_member(body:, id:, timeout_ms: nil, http_headers: nil)

def update_member(body:, id:, timeout_ms: nil, http_headers: nil)
  # update_member - Update Member
  # Update a member's role.
  #
  # Only available to owners and billing managers of team customers.
  #
  # Rules:
  # - Cannot modify your own role (to prevent self-demotion)
  # - Customer must have exactly one owner at all times
  request = Models::Operations::CustomerPortalMembersUpdateMemberRequest.new(
    id: id,
    body: body
  )
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = Utils.generate_url(
    Models::Operations::CustomerPortalMembersUpdateMemberRequest,
    base_url,
    "/v1/customer-portal/members/{id}",
    request
  )
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  req_content_type, data, form = Utils.serialize_request_body(request, false, false, :body, :json)
  headers["content-type"] = req_content_type
  raise StandardError, "request body is required" if data.nil? && form.nil?
  if form && !form.empty?
    body = Utils.encode_form(form)
  elsif Utils.match_content_type(req_content_type, "application/x-www-form-urlencoded")
    body = URI.encode_www_form(T.cast(data, T::Hash[Symbol, Object]))
  else
    body = data
  end
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "customer_portal:members:update_member",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).patch(url) do |req|
      req.body = body
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["200"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Components::CustomerPortalMember)
      response = Models::Operations::CustomerPortalMembersUpdateMemberResponse.new(
        status_code: http_response.status,
        content_type: content_type,
        raw_response: http_response,
        customer_portal_member: T.unsafe(obj)
      )
      return response
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["400", "401", "403", "404", "4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end