# frozen_string_literal: true

class AccountsController < ApplicationController
  include RememberDeviceConcern
  before_action :confirm_two_factor_authenticated
  before_action :confirm_user_is_not_suspended

  layout 'account_side_nav'

  def show
    analytics.account_visit
    session[:account_redirect_path] = account_path
    cacher = Pii::Cacher.new(current_user, user_session)
    @presenter = AccountShowPresenter.new(
      decrypted_pii: cacher.fetch(current_user.active_or_pending_profile&.id),
      sp_session_request_url: sp_session_request_url_with_updated_params,
      authn_context: resolved_authn_context_result,
      sp_name: decorated_sp_session.sp_name,
      sp_handoff_already_occurred: decorated_sp_session.successful_handoff?,
      user: current_user,
      locked_for_session: pii_locked_for_session?(current_user),
    )
    if session.delete(:from_select_email_flow)
      flash.now[:success] = t(
        'account.emails.confirmed_html',
        url: account_connected_accounts_url,
      )
    end
  end

  def reauthentication
    # This route sends a user through reauthentication and returns them to the account page, since
    # some actions within the account dashboard require a fresh reauthentication (e.g. managing an
    # MFA method or viewing verified profile information).
    user_session[:stored_location] = account_url(params.permit(:manage_authenticator))
    user_session[:context] = 'reauthentication'

    redirect_to login_two_factor_options_path
  end

  private

  def confirm_user_is_not_suspended
    redirect_to user_please_call_url if current_user.suspended?
  end
end
