
import {
    DataTypes
} from 'sequelize';

import {
    DBLogger,
    LabelDB
} from './db_conn.mjs'

export const Permission = LabelDB.define('permission', {
    permission: {
        type: DataTypes.STRING,
        allowNull: false,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    }
}, {
    timestamps: false
});


LabelDB.afterBulkSync(async () => {
    await Permission.bulkCreate([
        { 
            permission: 'settings',
            name: 'edit all Settings'
        },
        { 
            permission: 'settings.printer',
            name: 'edit Printers'
        },
        { 
            permission: 'settings.media',
            name: 'edit Medias'
        },
        { 
            permission: 'settings.queues',
            name: 'edit Queues'
        },
        { 
            permission: 'settings.labels',
            name: 'edit Lables'
        }
    ], {
        updateOnDuplicate: [
            'name'
        ]
    });
})


Permission.belongsTo(Permission, { as: 'parent', foreignKey: 'parentPermission' });
Permission.hasMany(Permission, { as: 'child', foreignKey: 'parentPermission' });
