class HardeningHardenSettings < ActiveRecord::Migration[5.2]
  def self.restore_setting(name)
    s = Setting.find_by(name: name)
    return if s.nil?

    s.state_current = s.state_initial
    s.save!
  end

  def self.set_setting(name, value)
    s = Setting.find_by(name: name)
    return if s.nil?

    s.state_current = { 'value' => value }
    s.save!
  end

  def self.up
    %w[ui_send_client_stats geo_ip_backend geo_location_backend image_backend
       geo_calendar_backend].each do |n|
      set_setting(n, '')
    end

    # disable customer ticket creation
    set_setting('customer_ticket_create', false)

    # disable user account registration
    set_setting('user_create_account', false)

    # bump up min password length
    set_setting('password_min_size', 10)

    # delete default zammad user
    nicole = User.find_by(email: 'nicole.braun@zammad.org')
    return if nicole.nil?

    Ticket.where(customer: nicole).destroy_all
    nicole.destroy
  end

  def self.down
    %w[ui_send_client_stats geo_ip_backend geo_location_backend image_backend
       geo_calendar_backend].each do |n|
      restore_setting(n)
    end
    %w[customer_ticket_create user_create_account password_min_size].each do |n|
      restore_setting(n)
    end
  end
end
