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

class MessageModel extends Model {
    static get schema() {
        return {
            dismissed: {type: Boolean, default: false},
            expires: {type: Date, default: () => {
                const date = new Date
                date.setYear(date.getUTCFullYear() + 1)
                return date
            }},
            display_type: {type: String, default: 'info'},
            message: String,
            user_id: String,
        }
    }

    static async for_user(user) {
        return this.find({ user_id: user.id, dismissed: false, expires: { $gt: new Date } })
    }

    static async create(user, message, display_type = 'info') {
        const msg = new this({
            message,
            display_type,
            user_id: user.id,
        })

        await msg.save()
        return msg
    }

    async dismiss() {
        this.dismissed = true
        return this.save()
    }
}

module.exports = exports = MessageModel
