# frozen_string_literal: true

module Decidim
  module System
    # A command that will create default help pages for an organization.
    class CreateDefaultHelpPages < Decidim::Command
      include Decidim::TranslatableAttributes

      # Public: Initializes the command.
      #
      # organization - An organization
      def initialize(organization)
        @organization = organization
      end

      # Executes the command.
      #
      # Returns nothing.
      def call
        ActiveRecord::Base.transaction do
          topic = Decidim::StaticPageTopic.create!(
            title: multi_translation("decidim.help.main_topic.title", organization: organization_name),
            description: multi_translation("decidim.help.main_topic.description", organization: organization_name),
            organization: @organization,
            show_in_footer: true,
            weight: 0
          )

          Decidim::StaticPage.create!(
            slug: "help",
            title: multi_translation("decidim.help.main_topic.default_page.title", organization: organization_name),
            content: multi_translation("decidim.help.main_topic.default_page.content", organization: organization_name),
            topic:,
            organization: @organization,
            weight: 0
          )

          Decidim.participatory_space_manifests.each do |manifest|
            scope = "decidim.help.participatory_spaces.#{manifest.name}"
            next unless I18n.exists?(scope)

            Decidim::StaticPage.create!(
              title: multi_translation("#{scope}.title"),
              content: multi_translation("#{scope}.page"),
              slug: manifest.name,
              topic:,
              organization: @organization
            )

            ContextualHelpSection.set_content(@organization, manifest.name, multi_translation("#{scope}.contextual"))
          end
        end
      end

      def multi_translation(key, **)
        Decidim::TranslationsHelper.multi_translation(key, @organization.available_locales, **)
      end

      def organization_name
        translated_attribute(@organization.name)
      end
    end
  end
end
