let crypto = require('crypto')

module.exports = (sequelize, DataTypes) => {
    let PassKey = sequelize.define('PassKey', {
        passkey: {
            type: DataTypes.STRING,
            required: true,
            defaultValue () {
                return crypto.randomBytes(64).toString('hex')
            },
            validate: {
                isString (val) {
                    if(typeof val !== 'string') {
                        throw new sequelize.ValidationError('passkey must be a string')
                    }
                }
            }
        }
    }, {
        instanceMethods: {
            isValid () {
                let ms = Date.now() - this.createdAt
                let dayMs = 1000*60*60*24

                //Has less than 1 day passed
                //since generating token?
                return ms / dayMs < 1
            }
        }
    })

    return PassKey
}
