# frozen_string_literal: true

require "spec_helper"

describe "Admin manages election census" do
  let(:manifest_name) { "elections" }
  let(:participatory_process) { create(:participatory_process, organization:) }
  let(:current_component) { create(:component, participatory_space: participatory_process, manifest_name: "elections") }
  let!(:election) { create(:election, component: current_component) }
  let!(:questions) { create_list(:election_question, 3) }
  let(:election_census_path) { Decidim::EngineRouter.admin_proxy(component).election_census_path(election) }
  let(:dashboard_path) { Decidim::EngineRouter.admin_proxy(component).dashboard_election_path(election) }

  include_context "when managing a component as an admin"

  before do
    visit election_census_path
  end

  it "opens the census tab" do
    expect(page).to have_content("Census type")
  end

  context "when the admin selects unregistered participants with tokens" do
    context "when the csv file is valid" do
      it "uploads the CSV file and creates participants" do
        select "Unregistered participants with tokens (fixed)", from: "census_manifest"
        expect(page).to have_content("Upload a CSV file")
        dynamically_attach_file("token_csv_file", Decidim::Dev.asset("valid_election_census.csv")) # with 2 users

        click_on "Save and continue" # redirects to the dashboard
        expect(page).to have_content("Census updated successfully")
        within ".main-tabs-menu__tabs li.is-active" do
          expect(page).to have_content("Dashboard")
        end

        visit election_census_path

        expect(page).to have_content("There are currently 2 people")
        expect(page).to have_content("The preview list is limited to 5 records.")
        expect(page).to have_content("user1@example.org")
        expect(page).to have_content("user2@example.org")
      end
    end

    context "when the csv file is invalid" do
      it "shows an error message" do
        select "Unregistered participants with tokens (fixed)", from: "census_manifest"
        expect(page).to have_content("Upload a CSV file")
        dynamically_attach_file("token_csv_file", Decidim::Dev.asset("census_with_missing_email.csv")) # has a row with missing email and 1 valid row

        click_on "Save and continue" # redirects to the dashboard
        within ".main-tabs-menu__tabs li.is-active" do
          expect(page).to have_content("Dashboard")
        end

        visit election_census_path

        expect(page).to have_content("There is currently 1 person")
        expect(page).to have_content("The preview list is limited to 5 records.")
      end
    end

    context "when the csv file has duplicate emails" do
      it "shows an error about duplicate records" do
        select "Unregistered participants with tokens (fixed)", from: "census_manifest"
        expect(page).to have_content("Upload a CSV file")
        dynamically_attach_file("token_csv_file", Decidim::Dev.asset("census_duplicate_emails.csv")) # has 3 the same rows

        click_on "Save and continue" # redirects to the dashboard
        within ".main-tabs-menu__tabs li.is-active" do
          expect(page).to have_content("Dashboard")
        end

        visit election_census_path

        expect(page).to have_content("There is currently 1 person")
        expect(page).to have_content("The preview list is limited to 5 records.")
        expect(page).to have_content("user1@example.org")
      end
    end
  end

  context "when the admin selects registered participants (dynamic)" do
    let!(:users) { create_list(:user, 10, :confirmed, organization:) }
    let(:authorized_users) { create_list(:user, 3, :confirmed, organization:) }
    let(:another_authorized_users) { create_list(:user, 2, :confirmed, organization:) }
    let(:available_authorizations) { %w(dummy_authorization_handler another_dummy_authorization_handler) }

    before do
      organization.update!(available_authorizations: available_authorizations)
    end

    context "when no verification handlers are selected" do
      it "shows all organization users in the preview" do
        select "Registered participants (dynamic)", from: "census_manifest"
        expect(page).to have_content("Additional required authorizations to vote (optional)")

        click_on "Save and continue" # redirects to the dashboard
        within ".main-tabs-menu__tabs li.is-active" do
          expect(page).to have_content("Dashboard")
        end

        visit election_census_path

        expect(page).to have_content("There are currently 11 people eligible for voting in this election (this might change on a dynamic census).") # 1 admin + 10 users
        expect(page).to have_content("The preview list is limited to 5 records.")
        expect(page).to have_css("table.table-list tbody tr", count: 5)
      end
    end

    context "when verification handlers are selected" do
      before do
        authorized_users.each do |user|
          create(:authorization, :granted, user:, name: "dummy_authorization_handler")
        end

        another_authorized_users.each do |user|
          create(:authorization, :granted, user:, name: "another_dummy_authorization_handler")
        end
      end

      it "shows only users with selected authorizations" do
        select "Registered participants (dynamic)", from: "census_manifest"
        expect(page).to have_content("Additional required authorizations to vote (optional)")

        check "Example authorization"
        fill_in "Allowed postal codes (separated by commas)", with: "08001, 08002, 08003"
        click_on "Save and continue" # redirects to the dashboard
        within ".main-tabs-menu__tabs li.is-active" do
          expect(page).to have_content("Dashboard")
        end

        visit election_census_path
        expect(page).to have_css("input[value='08001, 08002, 08003']")

        expect(page).to have_content("There are currently 3 people eligible for voting in this election (this might change on a dynamic census).")
        expect(page).to have_content("The preview list is limited to 5 records.")
        expect(page).to have_css("table.table-list tbody tr", count: 3)
        expect(election.reload.census_settings["authorization_handlers"]).to eq({
                                                                                  "dummy_authorization_handler" => {
                                                                                    "options" => {
                                                                                      "allowed_postal_codes" => "08001, 08002, 08003"
                                                                                    }
                                                                                  }
                                                                                })
        fill_in "Allowed postal codes (separated by commas)", with: "08002, 08003"
        click_on "Save and continue" # redirects to the dashboard
        expect(page).to have_current_path(dashboard_path)
        expect(election.reload.census_settings["authorization_handlers"]).to eq({
                                                                                  "dummy_authorization_handler" => {
                                                                                    "options" => {
                                                                                      "allowed_postal_codes" => "08002, 08003"
                                                                                    }
                                                                                  }
                                                                                })
      end

      context "when multiple verification handlers are selected" do
        let!(:user_with_multiple_authorizations) { create(:user, :confirmed, organization:) }

        before do
          create(:authorization, :granted, user: user_with_multiple_authorizations, name: "dummy_authorization_handler")
          create(:authorization, :granted, user: user_with_multiple_authorizations, name: "another_dummy_authorization_handler")
        end

        it "shows only users with all selected authorizations" do
          select "Registered participants (dynamic)", from: "census_manifest"
          expect(page).to have_content("Additional required authorizations to vote (optional)")

          check "Example authorization"
          check "Another example authorization"

          click_on "Save and continue" # redirects to the dashboard
          within ".main-tabs-menu__tabs li.is-active" do
            expect(page).to have_content("Dashboard")
          end

          visit election_census_path

          expect(page).to have_content("There is currently 1 person eligible for voting in this election (this might change on a dynamic census).")
          expect(page).to have_content("The preview list is limited to 5 records.")
          expect(page).to have_css("table.table-list tbody tr", count: 1)
        end
      end
    end
  end
end
