
import {
    DataTypes
} from 'sequelize';

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

export const Template = LabelDB.define('template', {
    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: 'templates',
    timestamps: false
});

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

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

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

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


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