const { Model } = require('flitter-orm')
const uuid = require('uuid/v4')

class DeviceTokenModel extends Model {
    static get schema() {
        return {
            user_id: String,
            create_date: { type: Date, default: () => new Date },
            expiration_date: {
                type: Date,
                default: () => {
                    const date = new Date();
                    date.setMonth(date.getMonth() + 1);
                    return date;
                }
            },
            token: {
                type: String,
                default: () => {
                    return Array(5).fill('').map(_ => uuid()).join('').replace(/-/g, '')
                }
            },
        }
    }

    static async grant_user(user) {
        const tok = new this({
            user_id: user.id,
        })

        await tok.save()
        return tok
    }
}

module.exports = exports = DeviceTokenModel
