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

module.exports = (sequelize) => {
    const DesignationVersionChild = sequelize.define('designationversionchild', {
        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(50), allowNull: false, unique: false },
        display_name: { type: DataTypes.STRING(100), allowNull: true },
        slug_name: { type: DataTypes.STRING(100), allowNull: true },
        description: { type: DataTypes.TEXT, allowNull: true },
        code: { type: DataTypes.STRING(20), allowNull: true },
        type: { type: DataTypes.STRING(20), allowNull: true },
        is_parent: { type: DataTypes.BOOLEAN, allowNull: true },

        // fk
        parent_id: { type: DataTypes.INTEGER, allowNull: true },
        designation_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'designations',
                key: 'id'
            }
        },
        company_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'companies',
                key: 'id'
            }
        },
        designationversion_id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'designationversions',
                key: 'id'
            }
        },

        // sappsv1 or erpnext
        ids: { type: DataTypes.INTEGER, allowNull: true },
    }, {
        tableName: 'designationversionchilds',
        freezeTableName: true
    });

    /**
     * Associates the DesignationVersionChild model with other models.
     * @param {object} models - The models to associate with.
     * @description
     *   - A DesignationVersionChild belongs to a DesignationVersion.
     *   - A DesignationVersionChild belongs to a Company.
     */
    DesignationVersionChild.associate = (models) => {
        /**
         * Associates the DesignationVersionChild model with the DesignationVersion model.
         * A DesignationVersionChild belongs to a DesignationVersion.
         * @param {object} models - The models to associate with.
         */
        DesignationVersionChild.belongsTo(models.DesignationVersion, { foreignKey: 'designationversion_id', as: 'designationversion' });
        /**
         * Associates the DesignationVersionChild model with the Company model.
         * A DesignationVersionChild belongs to a Company.
         * @param {object} models - The models to associate with.
         */
        DesignationVersionChild.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });

        /**
         * Associates the DesignationVersionChild model with the Designation model.
         * A DesignationVersionChild belongs to a Designation.
         * @param {object} models - The models to associate with.
         * @description
         *   This association is used to define the parent-children relationship between designations.
         *   The parent designation is the designation that contains this designation version child.
         *   The parent designation is used to generate the designation hierarchy.
         */
        DesignationVersionChild.belongsTo(models.Designation, { as: 'parent', foreignKey: 'parent_id' });
    };

    return DesignationVersionChild;
};

