// models/index.js
import { Sequelize } from 'sequelize';
import { getConfig } from "../config.mjs";
import { registerModels } from './models.mjs';

const configData = await getConfig();

// SQLite-Datenbank im data/-Verzeichnis
const sequelize = new Sequelize({
    dialect: 'mysql',
    host: configData.database.host,
    port: configData.database.port,
    database: configData.database.database,
    username: configData.database.username,
    password: configData.database.password,
    logging: false,
});

// Modelle initialisieren
const {
    Group,
    User
} = registerModels(sequelize);

// Datenbank synchronisieren
(async () => {
    try {
        await sequelize.authenticate();
        console.log('✓ MySQL connection successfull');
        // Automatische Migration
        await sequelize.sync({ alter: true, force: false });
        console.log('✓ MySQL migration finished');
    } catch (error) {
        console.error('✕ MySQL Error:', error);
    }
})()

export {
    Group,
    User
};