class TokenPolicy
  attr_reader :user, :token

  def initialize(user, token)
    @user = user || User.new
    @token = token
  end

  class Scope
    def initialize(user, scope)
      @user  = user
      @scope = scope
    end

    def resolve
      if user.admin?
        scope.all
      else
        scope.all
      end
    end

    private

    attr_reader :user, :scope
  end

  def index?
    true
  end

  def show?
    true
  end

  def create?
    @user.admin? || @user.signed_in?
  end

  def new?
    @user.admin? || @user.signed_in?
  end

  def add_existing?
    new?
  end

  def edit?
    @user.admin? || @token.ownable_id == @user.soft_token
  end

  def update?
    @user.admin? || edit?
  end

  def destroy?
    @user.admin? || @token.ownable_id == @user.soft_token
  end

  def refresh?
    @token.canister_id.present?
  end

  def launch?
    @user.admin? || @token.ownable_id == @user.soft_token
  end
end
