module ContentDataSetup

  def set_up_content_data!(num_categories = 3, num_content_downloads = 3, num_funnel_stages = 3)
    create_content_funnel_stages!(num_funnel_stages)
    create_content_categories!(num_categories)
    create_content_downloads!(num_content_downloads)

    ContentCategory.all.each do |category|
      category.update!(
        content_download_1_id: @content_download_1.id,
        content_download_2_id: @content_download_2.id
      )
    end
  end

  def create_content_funnel_stages!(n)
    (1..n).each { |i| ContentFunnelStage.create!(content_funnel_stage_params(i)) }
  end

  def create_content_categories!(n)
    (1..n).each { |i| ContentCategory.create!(content_category_params(i)) }
  end

  def create_content_downloads!(n)
    (1..n).each do |i|
      content_download = ContentDownload.create!(content_download_params(i))
      instance_variable_set("@content_download_#{i}", content_download)
    end
  end

  private

  def content_download_params(i)
    image = Upload.first || Upload.create!(
      category: "user",
      upload_type: "image",
      uploaded_by: User.first.id,
      url: "https://cdn.filepicker.io/api/file/g65lEfxrTaqAgouYOGym",
    )

    return {
      name: "Content Download #{i}",
      content_score: 1,
      upload: image,
      content_category: ContentCategory.first,
      content_funnel_stage: ContentFunnelStage.first,

      active: true,
      title_text: "Content Download #{i} - Title Text",
      description: "Content Download #{i} - Meta Description",

      landing_page_slug: "content-download-#{i}",
      landing_page_large_image: image,
      landing_page_background_image: image,
      landing_page_header: "Content Download #{i} - Landing Page Header",
      landing_page_button_text: "Content Download #{i} - Landing Page Button Text",
      landing_page_bullet_header: "Content Download #{i} - Landing Page Bullet Header",
      landing_page_bullet_points: "-a -b -c",
      landing_page_event_name: "content_download_#{i}_event_name",

      preview_card_image: image,

      hero_form_background_image: image,
      hero_form_header: "Content Download #{i} - Hero Form Header",
      hero_form_button_text: "Content Download #{i} - Hero Form Button Text"
    }
  end

  def content_category_params(i)
    {
      name: "Content Category #{i}",
      header: "Content Category #{i} Header",
      subheader: "Content Category #{i} Subheader",
      title_text: "Content Category #{i} Title Text",
      description: "Content Category #{i} Description",
      url: "/blog/category/content-category-#{i}",

      content_download_1_event_name: "Content Category #{i} Event Name",
      content_download_1_workflow_id: 1,

      content_download_2_event_name: "Content Category #{i} Event Name",
      content_download_2_workflow_id: 2,
    }
  end

  def content_funnel_stage_params(i)
    return {
      name: "Funnel Stage #{i}"
    }
  end
end
