
import { 
    Sequelize
} from 'sequelize';

import Logger from 'pino';

export var DBLogger = Logger(Logger.destination('./logs/database.log'))
DBLogger.level = 10;

export var LabelDB = new Sequelize({
    dialect: 'mysql',
    host: '10.10.30.10',
    port: "3306",
    username: "label_print",
    password: "mIhAha267oPiVoge1og5F2rub8nERI",
    database: "aol_label-print",
    logging: (Message) => DBLogger.debug(Message)
});


(async () => {
    await import('./media.model.mjs');
    await import('./printer.model.mjs');
    await import('./template.model.mjs');
    await import('./queue.model.mjs');
    await import('./perms.model.mjs');
    await import('./apps.model.mjs');
    await import('./job.model.mjs');

    try {
        await LabelDB.authenticate();
        DBLogger.info('Connection has been established successfully.');
    } catch (error) {
        DBLogger.error('Unable to connect to the database:', error);
    }
    
    try {
        await LabelDB.sync({ force: false, alter: true });
        DBLogger.info("All models were synchronized successfully.");
    } catch (error) {
        DBLogger.error(`Failed to synchronize models: ${error}`);
    }
})();
