<commit_before># frozen_string_literal: true

if !Rails.env.production? || ENV["SEED"]
  print "Creating seeds for decidim-core...\n" unless Rails.env.test?

  require "decidim/faker/localized"
  require "decidim/faker/internet"

  seeds_root = File.join(__dir__, "seeds")

  # Since we usually migrate and seed in the same process, make sure
  # that we don't have invalid or cached information after a migration.
  decidim_tables = ActiveRecord::Base.connection.tables.select do |table|
    table.starts_with?("decidim_")
  end
  decidim_tables.map do |table|
    table.tr("_", "/").classify.safe_constantize
  end.compact.each(&:reset_column_information)

  smtp_label = ENV["SMTP_FROM_LABEL"] || Faker::Twitter.unique.screen_name
  smtp_email = ENV["SMTP_FROM_EMAIL"] || Faker::Internet.email

  organization = Decidim::Organization.first || Decidim::Organization.create!(
    name: Faker::Company.name,
    twitter_handler: Faker::Hipster.word,
    facebook_handler: Faker::Hipster.word,
    instagram_handler: Faker::Hipster.word,
    youtube_handler: Faker::Hipster.word,
    github_handler: Faker::Hipster.word,
    smtp_settings: {
      from: "#{smtp_label} <#{smtp_email}>",
      from_email: smtp_email,
      from_label: smtp_label,
      user_name: ENV["SMTP_USERNAME"] || Faker::Twitter.unique.screen_name,
      encrypted_password: Decidim::AttributeEncryptor.encrypt(ENV["SMTP_PASSWORD"] || Faker::Internet.password(min_length: 8)),
      address: ENV["SMTP_ADDRESS"] || ENV["DECIDIM_HOST"] || "localhost",
      port: ENV["SMTP_PORT"] || ENV["DECIDIM_SMTP_PORT"] || "25"
    },
    host: ENV["DECIDIM_HOST"] || "localhost",
    external_domain_whitelist: ["decidim.org", "github.com"],
    description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.sentence(word_count: 15)
    end,
    default_locale: Decidim.default_locale,
    available_locales: Decidim.available_locales,
    reference_prefix: Faker::Name.suffix,
    available_authorizations: Decidim.authorization_workflows.map(&:name),
    users_registration_mode: :enabled,
    tos_version: Time.current,
    badges_enabled: true,
    user_groups_enabled: true,
    send_welcome_notification: true,
    file_upload_settings: Decidim::OrganizationSettings.default(:upload)
  )

  if organization.top_scopes.none?
    province = Decidim::ScopeType.create!(
      name: Decidim::Faker::Localized.literal("province"),
      plural: Decidim::Faker::Localized.literal("provinces"),
      organization: organization
    )

    municipality = Decidim::ScopeType.create!(
      name: Decidim::Faker::Localized.literal("municipality"),
      plural: Decidim::Faker::Localized.literal("municipalities"),
      organization: organization
    )

    3.times do
      parent = Decidim::Scope.create!(
        name: Decidim::Faker::Localized.literal(Faker::Address.unique.state),
        code: Faker::Address.unique.country_code,
        scope_type: province,
        organization: organization
      )

      5.times do
        Decidim::Scope.create!(
          name: Decidim::Faker::Localized.literal(Faker::Address.unique.city),
          code: "#{parent.code}-#{Faker::Address.unique.state_abbr}",
          scope_type: municipality,
          organization: organization,
          parent: parent
        )
      end
    end
  end

  territorial = Decidim::AreaType.create!(
    name: Decidim::Faker::Localized.literal("territorial"),
    plural: Decidim::Faker::Localized.literal("territorials"),
    organization: organization
  )

  sectorial = Decidim::AreaType.create!(
    name: Decidim::Faker::Localized.literal("sectorials"),
    plural: Decidim::Faker::Localized.literal("sectorials"),
    organization: organization
  )<commit_msg>Prevent Faker Address country_code from raising RetryLimitExceeded (#9036)<commit_after># frozen_string_literal: true

if !Rails.env.production? || ENV["SEED"]
  print "Creating seeds for decidim-core...\n" unless Rails.env.test?

  require "decidim/faker/localized"
  require "decidim/faker/internet"

  seeds_root = File.join(__dir__, "seeds")

  # Since we usually migrate and seed in the same process, make sure
  # that we don't have invalid or cached information after a migration.
  decidim_tables = ActiveRecord::Base.connection.tables.select do |table|
    table.starts_with?("decidim_")
  end
  decidim_tables.map do |table|
    table.tr("_", "/").classify.safe_constantize
  end.compact.each(&:reset_column_information)

  smtp_label = ENV["SMTP_FROM_LABEL"] || Faker::Twitter.unique.screen_name
  smtp_email = ENV["SMTP_FROM_EMAIL"] || Faker::Internet.email

  organization = Decidim::Organization.first || Decidim::Organization.create!(
    name: Faker::Company.name,
    twitter_handler: Faker::Hipster.word,
    facebook_handler: Faker::Hipster.word,
    instagram_handler: Faker::Hipster.word,
    youtube_handler: Faker::Hipster.word,
    github_handler: Faker::Hipster.word,
    smtp_settings: {
      from: "#{smtp_label} <#{smtp_email}>",
      from_email: smtp_email,
      from_label: smtp_label,
      user_name: ENV["SMTP_USERNAME"] || Faker::Twitter.unique.screen_name,
      encrypted_password: Decidim::AttributeEncryptor.encrypt(ENV["SMTP_PASSWORD"] || Faker::Internet.password(min_length: 8)),
      address: ENV["SMTP_ADDRESS"] || ENV["DECIDIM_HOST"] || "localhost",
      port: ENV["SMTP_PORT"] || ENV["DECIDIM_SMTP_PORT"] || "25"
    },
    host: ENV["DECIDIM_HOST"] || "localhost",
    external_domain_whitelist: ["decidim.org", "github.com"],
    description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do
      Decidim::Faker::Localized.sentence(word_count: 15)
    end,
    default_locale: Decidim.default_locale,
    available_locales: Decidim.available_locales,
    reference_prefix: Faker::Name.suffix,
    available_authorizations: Decidim.authorization_workflows.map(&:name),
    users_registration_mode: :enabled,
    tos_version: Time.current,
    badges_enabled: true,
    user_groups_enabled: true,
    send_welcome_notification: true,
    file_upload_settings: Decidim::OrganizationSettings.default(:upload)
  )

  if organization.top_scopes.none?
    province = Decidim::ScopeType.create!(
      name: Decidim::Faker::Localized.literal("province"),
      plural: Decidim::Faker::Localized.literal("provinces"),
      organization: organization
    )

    municipality = Decidim::ScopeType.create!(
      name: Decidim::Faker::Localized.literal("municipality"),
      plural: Decidim::Faker::Localized.literal("municipalities"),
      organization: organization
    )

    3.times do |time|
      parent = Decidim::Scope.create!(
        name: Decidim::Faker::Localized.literal(Faker::Address.unique.state),
        code: "#{Faker::Address.country_code}_#{time}",
        scope_type: province,
        organization: organization
      )

      5.times do
        Decidim::Scope.create!(
          name: Decidim::Faker::Localized.literal(Faker::Address.unique.city),
          code: "#{parent.code}-#{Faker::Address.unique.state_abbr}",
          scope_type: municipality,
          organization: organization,
          parent: parent
        )
      end
    end
  end

  territorial = Decidim::AreaType.create!(
    name: Decidim::Faker::Localized.literal("territorial"),
    plural: Decidim::Faker::Localized.literal("territorials"),
    organization: organization
  )

  sectorial = Decidim::AreaType.create!(
    name: Decidim::Faker::Localized.literal("sectorials"),
    plural: Decidim::Faker::Localized.literal("sectorials"),
    organization: organization
  )
{'fraction_non_alphanumeric': 0.1348492860920148, 'fraction_numerical': 0.002379693283976732, 'mean_word_length': 4.2976190476190474, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4789166', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2459, 'n_words': 396}