class Prime::Eth2Staking::CreatePositionWebhookWorker
  include Sidekiq::Worker
  # exponential retry backoff formula in seconds: (retry_count ** 4) + 15 + (rand(30) * (retry_count + 1))
  sidekiq_options retry: 13 # 13 is the approximate number of retries that will be performed in one day

  def perform(anjin_customer_id, anjin_position_id)
    customer = Prime::Eth2Staking::Customer.find_by(anjin_id: anjin_customer_id)
    position = Prime::Eth2Staking::Position.find_by(anjin_id: anjin_position_id)
    return unless customer && position

    client = Common::Client.new(customer.webhook_url, authorization: customer.webhook_api_key)
    body = {
      action: 'staking_position_created',
      data: {
        id: position.id,
        withdrawal_address: position.eth1_withdrawal_address,
        validators_count: position.validators_count
      }
    }
    client.post(nil, {}, body.to_json)
  end
end
