module Admin
  class BaseController < ActionController::Base
    layout "admin"
    protect_from_forgery with: :exception

    before_action :require_admin_login

    include ::AdminHelper
    helper_method :current_admin_account, :admin_logged_in?

    private

    def require_admin_login
      return if admin_logged_in?

      session[:admin_return_to] = request.fullpath unless request.path == admin_login_path
      redirect_to admin_login_path, alert: "Accedi per continuare"
    end

    def current_admin_account
      return unless session[:admin_account_id]

      @current_admin_account ||= AdminAccount.find_by(id: session[:admin_account_id])
    end

    def admin_logged_in?
      current_admin_account.present?
    end
  end
end
