require "rails_helper"

feature "Admin managing users" do
  let(:current_user) { create(:user, admin: true, email: "user@test.com") }
  let(:project) { create(:project, account: current_user.primary_account) }

  before do
    sign_in current_user
  end

  scenario "creates a new user" do
    visit admin_users_path

    click_link "New User"
    fill_in "Email", with: "bob@example.com"
    fill_in "Password", with: "password"
    select "My Team", from: "Account"
    check "Admin User"
    select "Account Manager", from: "Role"
    click_on "Create User"

    expect(page).to have_content "The user was created"
  end

  scenario "sees list of users" do
    visit admin_users_path

    expect(page).to have_content "user@test.com"
    expect(page).to have_content "account_manager"
    expect(page).to have_content current_user.primary_account.name
  end

  context "searchs for users" do
    before do
      create(:user, :with_name)
      create(:user, :with_different_name)
      visit admin_users_path
    end

    scenario "shows all users when query is empty", js: true do
      expect(page).to have_selector "form#search"
      expect(page).to have_content "John"
      expect(page).to have_content "Specimen"
    end

    scenario "shows only users matching search query", js: true do
      within "form#search" do
        fill_in "Search users", with: "John"
        click_on "button"
      end
      expect(page).to have_content "John"
      expect(page).not_to have_content "Specimen"
    end

    scenario "shows no users matching search query", js: true do
      within "form#search" do
        fill_in "Search users", with: "nonexisting"
        click_on "button"
      end
      expect(page).not_to have_content "John"
      expect(page).not_to have_content "Specimen"
    end
  end

  scenario "manages users" do
    another_user = create(:user, admin: false, email: "user2@test.com", accounts: current_user.accounts)
    note_author_user = create(:user, admin: false, email: "writer@test.com", accounts: current_user.accounts)
    create(:note, user: note_author_user, email: note_author_user.email)

    visit admin_users_path

    by "clicking Edit button can update existing users" do
      within "#user_#{another_user.id}" do
        click_on "Manage"
        click_link "Edit"
      end

      expect(page).to have_content("Edit User")

      fill_in "Email", with: "user-updated@example.com"
      check "Admin User"
      click_button "Update User"

      expect(page).not_to have_content("user2@test.com")

      click_link "Users"

      expect(page).to have_content("user-updated@example.com associate")
    end

    by "clicking Delete button can remove existing users" do
      sign_in current_user
      visit admin_users_path

      within "#user_#{another_user.id}" do
        click_on "Manage"
        click_link "Delete"
      end

      expect(page).not_to have_content "user2@test.com"
      expect(page).to have_content user_deleted_message
    end

    by "clicking Delete button can remote users who posted ntoes" do
      sign_in current_user
      visit admin_users_path

      within "#user_#{note_author_user.id}" do
        click_on "Manage"
        click_link "Delete"
      end

      expect(page).not_to have_content "writer@test.com"
      expect(page).to have_content user_deleted_message
    end
  end

  private

  def user_deleted_message
    t "admin.users.destroy.alert"
  end
end
