# frozen_string_literal: true

Gitlab::Experiment.configure do |config|

# Prefix all experiment names with a given value. Use `nil` for none.
config.name_prefix = nil

# The logger is used to log various details of the experiments.
config.logger = Logger.new($stdout)

# The base class that should be instantiated for basic experiments. It should
# be a string, so we can constantize it later.
config.base_class = 'ApplicationExperiment'

# The caching layer is expected to respond to fetch, like Rails.cache for
# instance -- or anything that adheres to ActiveSupport::Cache::Store.
config.cache = nil

# The domain to use on cookies.
#
# When not set, it uses the current host. If you want to provide specific
# hosts, you use `:all`, or provide an array.
#
# Examples:
#   nil, :all, or ['www.gitlab.com', '.gitlab.com']
config.cookie_domain = :all

# The default rollout strategy that works for single and multi-variants.
#
# You can provide your own rollout strategies and override them per
# experiment.
#
# Examples include:
#   Rollout::Random, or Rollout::RoundRobin
config.default_rollout = Gitlab::Experiment::Rollout::Percent

# Secret seed used in generating context keys.
#
# Consider not using one that's shared with other systems, like Rails'
# SECRET_KEY_BASE. Generate a new secret and utilize that instead.
@context_key_secret = nil

# Bit length used by SHA2 in generating context keys.
#
# Using a higher bit length would require more computation time.
#
# Valid bit lengths:
#   256, 384, or 512.
@context_key_bit_length = 256

# The default base path that the middleware (or rails engine) will be
# mounted. Can be nil if you don't want anything to be mounted automatically.
#
# This enables a similar behavior to how links are instrumented in emails.
#
# Examples:
#   '/-/experiment', '/redirect', nil
config.mount_at = '/experiment'

# When using the middleware, links can be instrumented and redirected
# elsewhere. This can be exploited to make a harmful url look innocuous or
# that it's a valid url on your domain. To avoid this, you can provide your
# own logic for what urls will be considered valid and redirected to.
#
# Expected to return a boolean value.
config.redirect_url_validator = lambda do |redirect_url|
  true
end

# Logic this project uses to determine inclusion in a given experiment.
#
# Expected to return a boolean value.
#
# This block is executed within the scope of the experiment and so can access
# experiment methods, like `name`, `context`, and `signature`.
config.inclusion_resolver = lambda do |requested_variant|
  false
end

# Tracking behavior can be implemented to link an event to an experiment.
#
# This block is executed within the scope of the experiment and so can access
# experiment methods, like `name`, `context`, and `signature`.
config.tracking_behavior = lambda do |event, args|
  # An example of using a generic logger to track events:
  config.logger.info "Gitlab::Experiment[#{name}] #{event}: #{args.merge(signature: signature)}"

  # Using something like snowplow to track events (in gitlab):
  #
  # Gitlab::Tracking.event(name, event, **args.merge(
  #   context: (args[:context] || []) << SnowplowTracker::SelfDescribingJson.new(
  #     'iglu:com.gitlab/gitlab_experiment/jsonschema/0-2-0', signature
  #   )
  # ))
end

# Called at the end of every experiment run, with the result.
#
# You may want to track that you've assigned a variant to a given context,
# or push the experiment into the client or publish results elsewhere like
# into redis.
#
# This block is executed within the scope of the experiment and so can access
# experiment methods, like `name`, `context`, and `signature`.
config.publishing_behavior = lambda do |result|
  # Track the event using our own configured tracking logic.
  track(:assignment)

  # Push the experiment knowledge into the front end. The signature contains
  # the context key, and the variant that has been determined.
  #
  # Gon.push({ experiment: { name => signature } }, true)

  # Log using our logging system, so the result (which can be large) can be
  # reviewed later if we want to.
  #
  # Lograge::Event.log(experiment: name, result: result, signature: signature)
end

end