require "rails_helper"

describe "Managed User" do
  context "Currently managed user" do
    scenario "No managed user" do
      login_as_manager
      visit management_document_verifications_path
      expect(page).not_to have_css ".account-info"
    end

    scenario "User is already level three verified" do
      user = create(:user, :level_three)

      login_as_manager
      visit management_document_verifications_path
      fill_in "document_verification_document_number", with: user.document_number
      click_button "Check document"

      expect(page).to have_content "already verified"

      within(".account-info") do
        expect(page).to have_content "Identified as"
        expect(page).to have_content user.username.to_s
        expect(page).to have_content user.email.to_s
        expect(page).to have_content user.document_number.to_s
      end
    end

    scenario "User becomes verified as level three" do
      user = create(:user, :level_two)

      login_as_manager
      visit management_document_verifications_path
      fill_in "document_verification_document_number", with: user.document_number
      click_button "Check document"

      expect(page).to have_content "Vote for budget projects"

      click_button "Verify"

      expect(page).to have_content "already verified"

      within(".account-info") do
        expect(page).to have_content "Identified as"
        expect(page).to have_content user.username.to_s
        expect(page).to have_content user.email.to_s
        expect(page).to have_content user.document_number.to_s
      end
    end

    scenario "User becomes verified as level two (pending email confirmation for level three)" do
      user = create(:user)

      login_as_manager
      visit management_document_verifications_path
      fill_in "document_verification_document_number", with: "12345678Z"
      click_button "Check document"

      within(".account-info") do
        expect(page).not_to have_content "Identified as"
        expect(page).not_to have_content "Username"
        expect(page).not_to have_content "Email"
        expect(page).to have_content "Document type"
        expect(page).to have_content "Document number"
        expect(page).to have_content "12345678Z"
      end

      expect(page).to have_content "Please introduce the email used on the account"

      fill_in "email_verification_email", with: user.email
      click_button "Send verification email"

      expect(page).to have_content "In order to completely verify this user, " \
                                   "it is necessary that the user clicks on a link"

      within(".account-info") do
        expect(page).to have_content "Identified as"
        expect(page).to have_content user.username.to_s
        expect(page).to have_content user.email.to_s
        expect(page).to have_content user.document_number.to_s
      end
    end

    scenario "User is created with email as level three from scratch" do
      login_as_manager

      visit management_document_verifications_path
      fill_in "document_verification_document_number", with: "12345678Z"
      click_button "Check document"

      expect(page).to have_content "Please introduce the email used on the account"

      click_link "Create a new account"

      fill_in "user_username", with: "pepe"
      fill_in "user_email", with: "pepe@gmail.com"

      click_button "Create user"

      expect(page).to have_content "We have sent an email"
      expect(page).not_to have_content "Autogenerated password is"

      within(".account-info") do
        expect(page).to have_content "Identified as"
        expect(page).to have_content "pepe"
        expect(page).to have_content "pepe@gmail.com"
        expect(page).to have_content "12345678Z"
      end
    end

    scenario "User is created without email as level three from scratch" do
      login_as_manager

      visit management_document_verifications_path
      fill_in "document_verification_document_number", with: "12345678Z"
      click_button "Check document"

      expect(page).to have_content "Please introduce the email used on the account"

      click_link "Create a new account"

      fill_in "user_username", with: "peppa"
      fill_in "user_email", with: ""

      click_button "Create user"

      expect(page).not_to have_content "We have sent an email"
      expect(page).to have_content "Autogenerated password is"

      within(".account-info") do
        expect(page).to have_content "Identified as"
        expect(page).to have_content "peppa"
        expect(page).to have_content "12345678Z"
      end
    end
  end

  scenario "Close the currently managed user session" do
    user = create(:user, :level_three)

    login_as_manager
    visit management_document_verifications_path
    fill_in "document_verification_document_number", with: user.document_number
    click_button "Check document"

    expect(page).to have_content "already verified"

    within(".account-info") do
      expect(page).to have_content "Identified as"
      expect(page).to have_content user.username.to_s

      click_button "Change user"
    end

    expect(page).to have_content "User session signed out successfully."
    expect(page).not_to have_content "Identified as"
    expect(page).not_to have_content user.username.to_s
    expect(page).to have_current_path(management_root_path)
  end
end
