
import {
    DataTypes
} from 'sequelize';

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

export const Label = LabelDB.define('label', {
    id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    width: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    height: {
        type: DataTypes.INTEGER,
        allowNull: false
    }
}, {
    tableName: 'labels',
    timestamps: false
});

export const Element = LabelDB.define('element', {
    id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true
    },
    type: {
        type: DataTypes.STRING,
        allowNull: false
    },
    config: {
        type: DataTypes.JSON,
        allowNull: false
    },
    comment: {
        type: DataTypes.TEXT,
        allowNull: false
    }
}, {
    tableName: 'label_elements',
    timestamps: false
});

export const Variable = LabelDB.define('variable', {
    id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    regex: {
        type: DataTypes.TEXT,
        allowNull: false
    },
    comment: {
        type: DataTypes.TEXT,
        allowNull: false
    }
}, {
    tableName: 'label_variables',
    timestamps: false
});

Element.belongsTo(Label);
Label.hasMany(Element, {
    onDelete: 'CASCADE',
    foreignKey: {
        allowNull: false
    }
});

Variable.belongsTo(Label);
Label.hasMany(Variable, {
    onDelete: 'CASCADE',
    foreignKey: {
        allowNull: false
    }
});


console.log(Label.associations);
for (let assoc of Object.keys(Label.associations)) {
    for (let accessor of Object.keys(Label.associations[assoc].accessors)) {
        console.log(Label.name + '.' + Label.associations[assoc].accessors[accessor] + '()');
    }
}