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

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

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

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

        await msg.save()
        return msg
    }

    async mark_seen() {
        this.seen = true
        return this.save()
    }
}

module.exports = exports = LoginMessageModel
