# spec/support/features/session_helpers.rb
module Features
  module SessionHelpers

    def sign_up_as user
      visit signup_path
      fill_in 'user_first_name', with: user.first_name
      fill_in 'user_last_name', with: user.last_name
      fill_in 'Username', with: user.username
      fill_in 'Email', with: user.email
      fill_in 'user_password', with: user.password
      fill_in 'user_password_confirmation', with: user.password_confirmation
      check 'user_agree_policy_terms'
      InvisibleCaptcha.timestamp_threshold = 1
      sleep InvisibleCaptcha.timestamp_threshold
      click_button 'Sign Up'
    end

    def sign_in(user = nil)
      user ||= FactoryBot.create(:user)
      visit signin_path
      fill_in "Email or Username", with: [user.email,user.username].sample
      fill_in "Password", with: "password"
      click_button "Sign in"
    end

    def sign_out(user = nil)
      user ||= FactoryBot.create(:user)
      visit signout_path
    end

    def sign_in_admin_for resource
      admin = FactoryBot.create(:user)
      admin.add_role :admin, resource
      sign_in admin
    end

    def sign_in_superadmin
      superadmin = FactoryBot.create(:user, created_at: 1.year.ago)
      superadmin.add_role :superadmin
      sign_in superadmin
    end

  end
end
