let sharp = require('sharp')

module.exports = (sequelize, DataTypes) => {
	let teamPicture = sequelize.define('teamPicture', {
		file: DataTypes.BLOB('long'),
		mimetype: DataTypes.STRING
	}, {
		classMethods: {
			associate (models) {
				teamPicture.belongsTo(models.Team)
			}
		},
		hooks: {
			beforeUpdate (teamPicture, options, cb) {
				sharp(teamPicture.file)
					.resize(256, 256, { fit: 'cover' })
					.toBuffer((err, buff) => {
						teamPicture.file = buff

						cb(err || null, options)
					})
			}
		}
	})

	return teamPicture
}