module WolfCore::Integrations::WebhooksOperations

def build_body_from_params(params:, id_key:)

def build_body_from_params(params:, id_key:)
  {
    event_name: get_event_name(params: params),
    id_key => get_object_id(params: params)
  }
end

def get_event_name(params:)

def get_event_name(params:)
  message = JSON.parse(params['Message'] || {})
  message['event_name']
end

def get_event_type(params:)

def get_event_type(params:)
  params['Type']
end

def get_object_id(params:)

def get_object_id(params:)
  params = JSON.parse(params['Message'] || '{}')
  params['object_id']
end

def get_payload(params:, event_type: nil)

def get_payload(params:, event_type: nil)
  event_type ||= get_event_type(params: params)
  return if event_type == 'SubscriptionConfirmation'
  params = JSON.parse(params['Message'] || '{}')
  JSON.parse(params['payload'] || '{}')
end

def group_custom_values_by_custom_requirement_group(

def group_custom_values_by_custom_requirement_group(
  payload:,
  jobseeker_file_custom_requirement_ids:,
  reference_form_custom_requirement_ids:,
  disclosure_form_custom_requirement_ids:
)
  log_object payload, title: 'group_custom_values_by_custom_requirement_group payload is'
  log_object jobseeker_file_custom_requirement_ids, title: 'group_custom_values_by_custom_requirement_group jobseeker_file_custom_requirement_ids are'
  log_object reference_form_custom_requirement_ids, title: 'group_custom_values_by_custom_requirement_group reference_form_custom_requirement_ids are'
  log_object disclosure_form_custom_requirement_ids, title: 'group_custom_values_by_custom_requirement_group disclosure_form_custom_requirement_ids are'
  custom_values = payload.dig('changes', 'custom_values')
  log_object custom_values, title: 'group_custom_values_by_custom_requirement_group custom_values 1 are'
  if custom_values.blank?
    custom_values = [ payload['attributes'] ]
    log_object custom_values, title: 'group_custom_values_by_custom_requirement_group custom_values 2 are'
  end
  custom_values.group_by do |custom_value|
    log_object custom_value, title: 'group_custom_values_by_custom_requirement_group custom_value is'
    custom_requirement_group = if jobseeker_file_custom_requirement_ids.include?(custom_value['custom_requirement_id'])
      'jobseeker_file'
    elsif reference_form_custom_requirement_ids.include?(custom_value['custom_requirement_id'])
      'reference_form'
    elsif disclosure_form_custom_requirement_ids.include?(custom_value['custom_requirement_id'])
      'disclosure_form'
    elsif custom_value['user_category'] == 'Freelancer'
      'jobseeker'
    elsif custom_value['user_category'] == 'Campaign'
      'order'
    else
      'unknown_group'
    end
    custom_requirement_group
  end
end

def process_webhook(params:)

def process_webhook(params:)
  event_type = get_event_type(params: params)
  event_name = get_event_name(params: params)
  if event_type == 'SubscriptionConfirmation'
    url = params['SubscribeURL']
  elsif event_type == 'Notification'
    yield(event_name)
  end
end

def subscription_confirmation_request(url:)

def subscription_confirmation_request(url:)
  safe_http_get(
    url: url,
    error_message: "Can not confirm subscription",
  )
end

def validate_user_is_not_admin(params:)

def validate_user_is_not_admin(params:)
  event_type = get_event_type(params: params)
  return if event_type == 'SubscriptionConfirmation'
  user_id = params.dig('invoker', 'user_id').to_s
  return unless user_id == ENV['WOLF_ADMIN_ID'].to_s
  raise_service_error("Ignoring event from user id #{user_id}")
end