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

class OfflineDataSyncModel extends Model {
    static get schema() {
        return {
            user_id: String,
            sync_timestamp: { type: Date, default: () => new Date() },
            UUID: { type: String, default: uuid },
            codiums: [Object],
            databases: [Object],
            databaseColumns: [Object],
            databaseEntries: [Object],
            fileGroups: [Object],
            pages: [Object],
            pageNodes: [Object],
        }
    }

    static async from_request(req) {
        const rec = new this({
            user_id: req.user.id,
            codiums: req.body?.dirtyRecords.codiums,
            databases: req.body?.dirtyRecords.databases,
            databaseColumns: req.body?.dirtyRecords.databaseColumns,
            databaseEntries: req.body?.dirtyRecords.databaseEntries,
            fileGroups: req.body?.dirtyRecords.fileGroups,
            pages: req.body?.dirtyRecords.pages,
            pageNodes: req.body?.dirtyRecords.pageNodes,
        })

        await rec.save()
        return rec
    }
}

module.exports = exports = OfflineDataSyncModel
