# frozen_string_literal: true

describe CustomWizard::AdminWizardController do
  fab!(:admin_user) { Fabricate(:user, admin: true) }
  fab!(:user1) { Fabricate(:user) }
  fab!(:user2) { Fabricate(:user) }
  let(:template) { get_wizard_fixture("wizard") }
  let(:category) do
    Fabricate(
      :category,
      custom_fields: {
        create_topic_wizard: template["name"].parameterize(separator: "_"),
      },
    )
  end

  before do
    enable_subscription("standard")
    CustomWizard::Template.save(template, skip_jobs: true)
    template_2 = template.dup
    template_2["id"] = "super_mega_fun_wizard_2"
    CustomWizard::Template.save(template_2, skip_jobs: true)

    template_3 = template.dup
    template_3["id"] = "super_mega_fun_wizard_3"
    template_3["after_signup"] = true
    CustomWizard::Template.save(template_3, skip_jobs: true)

    sign_in(admin_user)
  end

  it "returns a basic list of wizard templates and wizard field types" do
    get "/admin/wizards/wizard.json"
    expect(response.parsed_body["wizard_list"].map { |w| w["id"] }).to match_array(
      %w[super_mega_fun_wizard super_mega_fun_wizard_2 super_mega_fun_wizard_3],
    )
    expect(response.parsed_body["field_types"].keys).to eq(
      CustomWizard::Field.types.keys.map(&:to_s),
    )
  end

  it "returns a wizard template" do
    get "/admin/wizards/wizard/#{template["id"]}.json"
    expect(response.parsed_body["id"]).to eq(template["id"])
    expect(response.parsed_body["steps"].length).to eq(3)
  end

  it "removes wizard templates whilst making sure create_topic_wizard settings for that wizard are removed from Categories" do
    expect(
      CategoryCustomField.find_by(
        category_id: category.id,
        name: "create_topic_wizard",
        value: template["name"].parameterize(separator: "_"),
      ),
    ).not_to eq(nil)
    delete "/admin/wizards/wizard/#{template["id"]}.json"
    expect(response.status).to eq(200)
    expect(CustomWizard::Template.exists?(template["id"])).to eq(false)
    expect(
      CategoryCustomField.find_by(
        name: "create_topic_wizard",
        value: template["name"].parameterize(separator: "_"),
      ),
    ).to eq(nil)
  end

  it "saves wizard templates" do
    template_updated = template.dup
    template_updated["name"] = "Super Mega Fun Wizard 2"
    template_updated["multiple_submissions"] = false
    template_updated["steps"][0]["fields"][0]["label"] = "Text 2"

    put "/admin/wizards/wizard/#{template["id"]}.json", params: { wizard: template_updated }
    expect(response.status).to eq(200)

    updated_template = CustomWizard::Template.find("super_mega_fun_wizard")
    expect(updated_template["name"]).to eq("Super Mega Fun Wizard 2")
    expect(updated_template["multiple_submissions"]).to eq("false")
    expect(updated_template["steps"][0]["fields"][0]["label"]).to eq("Text 2")
  end
end
