const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const DesignationVersions = sequelize.define('designationversion', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        creation: { type: DataTypes.DATE, allowNull: false },
        modified: { type: DataTypes.DATE, allowNull: false },
        modified_by: { type: DataTypes.INTEGER, allowNull: false },
        owner: { type: DataTypes.INTEGER, allowNull: false },
        enabled: { type: DataTypes.BOOLEAN, allowNull: false },

        name: { type: DataTypes.STRING(100), allowNull: false, unique: true },
        display_name: { type: DataTypes.STRING(100), allowNull: false },
        slug_name: { type: DataTypes.STRING(100), allowNull: true },
        valid_from: { type: DataTypes.DATEONLY, allowNull: false },
        valid_to: { type: DataTypes.DATEONLY, allowNull: false },

        // fk
        company_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'Companies',
                key: 'id'
            }
        },
    }, {
        tableName: 'designationversions',
        freezeTableName: true
    });

    /**
     * Associates the DesignationVersions model with other models.
     */
    DesignationVersions.associate = (models) => {
        /**
         * Associates the DesignationVersions model with the DesignationVersionChild model.
         * A DesignationVersion has many DesignationVersionChilds.
         */
        DesignationVersions.hasMany(models.DesignationVersionChild, { foreignKey: 'designationversion_id', as: 'designationversionchilds' });

        /**
         * Associates the DesignationVersions model with the Company model.
         * A DesignationVersion belongs to a Company.
         */
        DesignationVersions.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    }

    return DesignationVersions;
};

