require "rails_helper"

feature "Account manager managing account members" do
  scenario "promoting an asssociate to project manager" do
    account = create(:account)
    account_manager = create(:user, :account_manager, primary_account: account)
    associate = create(:user, :associate, primary_account: account, email: "associate.user@test.com")

    sign_in account_manager
    visit account_auths_path

    expect(page).to have_content "Associate"

    within "#account_auth_#{associate.account_auths.first.id}" do
      select "Project Manager", from: "Role"
      click_on "Change Role"
    end

    expect(page).to have_content "Role has been updated"
    expect(page).to have_content "Project Manager"
  end

  scenario "demoting a project manager to associate" do
    account = create(:account)
    account_manager = create(:user, :account_manager, primary_account: account)
    associate = create(:user, :manager, primary_account: account, email: "associate.user@test.com")

    sign_in account_manager
    visit account_auths_path

    expect(page).to have_content "Project Manager"

    within "#account_auth_#{associate.account_auths.first.id}" do
      select "Associate", from: "Role"
      click_on "Change Role"
    end

    expect(page).to have_content "Role has been updated"
    expect(page).to have_content "Associate"
  end

  scenario "promoting an asssociate to account manager" do
    account = create(:account)
    account_manager = create(:user, :account_manager, primary_account: account)
    associate = create(:user, :associate, primary_account: account, email: "associate.user@test.com")

    sign_in account_manager
    visit account_auths_path

    expect(page).to have_content "Associate"

    within "#account_auth_#{associate.account_auths.first.id}" do
      select "Account Manager", from: "Role"
      click_on "Change Role"
    end

    expect(page).to have_content "Role has been updated"
    expect(page).to have_content "Account Manager"
  end

  scenario "demoting an account manager to associate" do
    account = create(:account)
    account_manager = create(:user, :account_manager, primary_account: account)
    associate = create(:user, :account_manager, primary_account: account, email: "associate.user@test.com")

    sign_in account_manager
    visit account_auths_path

    expect(page).to have_content "Account Manager"

    within "#account_auth_#{associate.account_auths.first.id}" do
      select "Associate", from: "Role"
      click_on "Change Role"
    end

    expect(page).to have_content "Role has been updated"
    expect(page).to have_content "Associate"
  end

  scenario "promoting a project_manager to account manager" do
    account = create(:account)
    account_manager = create(:user, :account_manager, primary_account: account)
    associate = create(:user, :manager, primary_account: account, email: "associate.user@test.com")

    sign_in account_manager
    visit account_auths_path

    expect(page).to have_content "Project Manager"

    within "#account_auth_#{associate.account_auths.first.id}" do
      select "Account Manager", from: "Role"
      click_on "Change Role"
    end

    expect(page).to have_content "Role has been updated"
    expect(page).to have_content "Account Manager"
  end

  scenario "demoting an account manager to associate" do
    account = create(:account)
    account_manager = create(:user, :account_manager, primary_account: account)
    associate = create(:user, :account_manager, primary_account: account, email: "associate.user@test.com")

    sign_in account_manager
    visit account_auths_path

    expect(page).to have_content "Account Manager"

    within "#account_auth_#{associate.account_auths.first.id}" do
      select "Project Manager", from: "Role"
      click_on "Change Role"
    end

    expect(page).to have_content "Role has been updated"
    expect(page).to have_content "Project Manager"
  end
end
