class Admin::ApplicationController < ActionController::Base
  include Authentication
  include Pundit::Authorization
  include Pagy::Backend

  around_action :set_time_zone
  before_action :authenticate_user!

  rescue_from Pundit::NotAuthorizedError, with: :deny_access
  rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

  layout -> { turbo_frame_request? ? false : "admin" }

  private

  def set_time_zone
    Time.use_zone("Beijing") { yield }
  end

  # Show no permisson info when user visit a page which not authorized
  def deny_access
    render "admin/deny_access"
  end

  def record_not_found
    flash[:alert] = "Record not found"
    redirect_to admin_root_path
  end
end
