text: const mongoose = require('mongoose')
  const { MEMBER_RANKS } = require('./group-mappings')
  const MEMBER_ACCEPTANCE = require('./group-mappings').MEMBER_ACCEPTANCE
  const GROUP_VISIBILITY = require('./group-mappings').GROUP_VISIBILITY
  
  const HTML_CSS_LIMIT = 131072
  const DESCRIPTION_LIMIT = 1024
  const JS_LIMIT = 65536
  const MEMBER_LIMIT = 200
  const INVITE_LIMIT = 100
  
  /* Membership rules 
  	RANKS
  	- Ranks are account wide.
  	- The account who creates the group is automatically the OWNER
  	- The account's rank is determined by the highest rank of the character 
  		they have in the group
  	- Only one account's character can have a rank above MEMBER to avoid
  		confusion when performing group actions.
  	- OWNERs cannot demote themselves unless they're specifying another 
  		OWNER.
  	- When an OWNER transfers their rank, they are given the ADMIN rank.
  
  	ADDING MEMBERS
  	- If an account does not have membership already, they will join based
  		on the membership acceptance rules.
  	- If an account is already a member, they can add additional characters
  		regardless of membership acceptance rules.
  
  	REMOVING MEMBERS
  	- As long as an account has one character in the group, their membership
  		remains.
  	- Accounts cannot remove a character that has a rank other than MEMBER
  		unless it's their last one
  	- If an account deletes their last character, their rank is revoked unless
  		they're the owner.
  	- If an account is an owner, they cannot delete their last character until
  		they pass ownership to another member.
  
  */
  let ranks = {
  	"9aa09482487fd93b48d2ffac": {
  		rank: MEMBER_RANKS.MEMBER,
  		character: "4febd75ba131925f9e8e1a63"
  	}
  }
  
  var groupSchema = new mongoose.Schema({
  	/* User settable attributes */
  	groupName: {
  		type: String,
  		trim: true,
  		unique: true
  	},
  
  	description: { 
  		type: String,
  		default: '',
  		validate: [(val) => {
  			return val.length <= DESCRIPTION_LIMIT;
  		}, `Invites exceeds the limit of ${DESCRIPTION_LIMIT}`]
  	},
  
  	members: {
  		type: mongoose.Schema.Types.Mixed,
  		default: {},
  		validate: [val => {return Object.keys(val).length <= MEMBER_LIMIT}, 
  			`Cannot add another member, limit of ${MEMBER_LIMIT} exceeded`]
  	},
  
  	bannedMembers: {
  		type: mongoose.Schema.Types.Mixed,
  		default: {}
  	},
  
  	inviteKey: {
  		type: String,
  		trim: true,
  		unique: true
  	},
  
  	memberCount: {
  		type: Number,
  		default: 1
  	},
  
  	memberAcceptance: {
  		type: Number,
  		default: MEMBER_ACCEPTANCE.APPROVAL_NEEDED
  	},
  
  	groupVisibility: { 
  		type: Number,
  		default: GROUP_VISIBILITY.VISIBLE
  	},
  
  	profileHtml: {
  		type: String,
  		default: '',
  		validate: [(val) => {
  			return val.length <= HTML_CSS_LIMIT;	
  		}, `HTML exceeds the limit of ${HTML_CSS_LIMIT}`]
  	},
  
  	profileCss: {
  		type: String,
  		default: '',
  		validate: [(val) => {
  			return val.length <= HTML_CSS_LIMIT;
  		}, `CSS exceeds the limit of ${HTML_CSS_LIMIT}`]
  	},
  
  	profileJs: {
  		type: String,
  		default: '',
  		validate: [(val) => {
  			return val.length <= JS_LIMIT;
  		}, `JS exceeds the limit of ${JS_LIMIT}`]
  	}
  }, 
  { collation: { locale: 'en_US', strength: 2 },
    timestamps: true})
  
  let groupInvites = new mongoose.Schema({
  	inviteKey: {
  		type: String,
  		trim: true,
  		unique: true
  	},
  
  	groupName: {
  		type: String,
  		trim: true,
  	},
  
  	keyExpiration: {
  		type: Date,
  		default: Date.now,
  		expires: 0
  	},
  },
  {timestamps: true})
  
  mongoose.model('Groups', groupSchema)
  mongoose.model('GroupInvites', groupInvites)