module.exports = (sequelize, DataTypes) => {
	let Ip = sequelize.define('Ip', {
		ip: {
			type: DataTypes.STRING(45),
			validate: {
				isIP: true
			}
		}
	})
	Ip.associate = function (models) {
		Ip.belongsToMany(models.User, { through: 'UserIp' })
	}
	Ip.createIfNotExists = function (ipAddress, user) {
		let existingIp = Ip.findOne({
			where: { ip: ipAddress },
			include: [{
				model: sequelize.models.User,
				where: { id: user.id }
			}]
		})

		if(!existingIp) {
			let ip = Ip.create({ ip: ipAddress })
			ip.addUser(user)
		}
	}
	return Ip
}
