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

class NotifyConfigModel extends Model {
    static get services() {
        return [...super.services, 'notify', 'configs', 'jobs']
    }

    static get schema() {
        return {
            user_id: String,
            gateway_url: String,
            application_key: String,
            created_on: { type: Date, default: () => new Date },
            active: { type: Boolean, default: true },
        }
    }

    async to_api() {
        return {
            user_id: this.user_id,
            gateway_url: this.gateway_url,
            application_key: this.application_key,
            created_on: this.created_on,
            active: this.active,
        }
    }

    async send({ title = '', message, priority = 5 }) {
        if ( !title ) title = this.configs.get('app.name')
        this.notify.host = this.gateway_url
        await this.notify.send_one(this.application_key, title, message, priority)
        this.notify.host = false
    }

    async log({ title = '', message, priority = 5 }) {
        await this.jobs.queue('notifications').add('PushNotify', {
            title, message, priority, user_id: this.user_id,
        })
    }
}

module.exports = exports = NotifyConfigModel
