# frozen_string_literal: true

DiscourseEvent.on(:site_setting_changed) do |name, old_value, new_value|
  Category.clear_subcategory_ids if name === :max_category_nesting

  # Enabling `must_approve_users` on an existing site is odd, so we assume that the
  # existing users are approved unless they currently have a pending reviewable.
  if name == :must_approve_users && new_value == true
    User
      .where(approved: false)
      .joins(
        "LEFT JOIN reviewables r ON r.target_id = users.id AND r.target_type = 'User' AND r.status = #{Reviewable.statuses[:pending]}",
      )
      .where(r: { id: nil })
      .update_all(approved: true)
  end

  if name == :emoji_set
    Emoji.clear_cache

    before = "/images/emoji/#{old_value}/"
    after = "/images/emoji/#{new_value}/"

    Scheduler::Defer.later("Fix Emoji Links") do
      DB.exec(
        "UPDATE posts SET cooked = REPLACE(cooked, :before, :after) WHERE cooked LIKE :like",
        before: before,
        after: after,
        like: "%#{before}%",
      )
    end
  end

  Stylesheet::Manager.clear_color_scheme_cache! if %i[base_font heading_font].include?(name)

  Report.clear_cache(:storage_stats) if %i[backup_location s3_backup_bucket].include?(name)

  if name == :slug_generation_method
    Scheduler::Defer.later("Null topic slug") { Topic.update_all(slug: nil) }
  end

  if name.to_s.include?("_icon") ||
       %i[icon objects].include?(SiteSetting.type_supervisor.get_type(name))
    SvgSprite.expire_cache
  end

  SiteIconManager.ensure_optimized! if SiteIconManager::WATCHED_SETTINGS.include?(name)

  # Make sure medium and high priority thresholds were calculated.
  if name == :reviewable_low_priority_threshold && Reviewable.min_score_for_priority(:medium) > 0
    Reviewable.set_priorities(low: new_value)
  end

  Emoji.clear_cache && Discourse.request_refresh! if name == :emoji_deny_list

  Discourse.clear_urls! if %i[tos_topic_id privacy_topic_id].include?(name)

  # Update seeded topics
  if %i[title site_description].include?(name)
    topics = SeedData::Topics.with_default_locale
    topics.update(site_setting_names: ["welcome_topic_id"], skip_changed: true)
  end

  Theme.expire_site_cache! if name == :default_theme_id

  if name == :splash_screen_image && new_value.present?
    SiteSetting::SplashScreenImageChanged.call(
      upload_id: new_value,
      guardian: Discourse.system_user.guardian,
    ) do |result|
      on_model_not_found(:upload) { Rails.logger.error("Upload not found for #{name} change") }
      on_model_not_found(:svg) do
        Rails.logger.error("SVG could not be parsed from upload #{new_value} when updating #{name}")
      end
      on_success { Rails.logger.info("Successfully updated #{name} SVG") }
      on_failure { Rails.logger.error("Failed to update #{name} SVG") }
    end
  end

  if name == :simple_email_subject
    SiteSetting::Action::SimpleEmailSubjectToggled.call(enabled: new_value)
  end

  if name == :content_localization_enabled && new_value == true
    %i[post_menu post_menu_hidden_items].each do |setting_name|
      current_items = SiteSetting.get(setting_name).split("|")
      if current_items.exclude?("addTranslation")
        edit_index = current_items.index("edit")
        insert_position = edit_index ? edit_index + 1 : 0
        current_items.insert(insert_position, "addTranslation")
        SiteSetting.set(setting_name, current_items.join("|"))
      end
    end
  end

  # Update Discourse ID metadata
  if SiteSetting.discourse_id_client_id.present? && SiteSetting.discourse_id_client_secret.present?
    if %i[title logo logo_small site_description].include?(name)
      Scheduler::Defer.later("Update Discourse ID metadata") do
        DiscourseId::Register.call(update: true)
      rescue StandardError => e
        Rails.logger.error(
          "Failed to update Discourse ID metadata after #{name} change: #{e.message}",
        )
      end
    end
  end
end
