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

module.exports = (sequelize) => {
    const TerritoryItemGroup = sequelize.define('territoryitemgroup', {
        territory_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'territories', 
                key: 'id',           
                primaryKey: true, 
            }
        },
        itemgroup_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'itemgroups', 
                key: 'id',          
                primaryKey: true, 
            }
        }
    }, {
        tableName: 'territoryitemgroups',
        freezeTableName: true,
    });

    /**
     * Associates TerritoryItemGroup with other models.
     * @param {object} models - The models to associate with.
     * @description
     *   - A TerritoryItemGroup belongs to a Territory.
     *   - A TerritoryItemGroup belongs to an ItemGroup.
     *   - The association is labeled as 'itemgroup'.
     */
    TerritoryItemGroup.associate = (models) => {
        /**
         * Associate TerritoryItemGroup with Territory
         * @param {object} models - The models to associate with.
         * @description
         *   - A TerritoryItemGroup belongs to a Territory.
         */
        TerritoryItemGroup.belongsTo(models.Territory, {
            foreignKey: 'territory_id',
            as: 'territory'
        });
        
        /**
         * Associate TerritoryItemGroup with ItemGroup
         * @param {object} models - The models to associate with.
         * @description
         *   - A TerritoryItemGroup belongs to an ItemGroup.
         *   - The association is labeled as 'itemgroup'.
         */
        TerritoryItemGroup.belongsTo(models.ItemGroup, {
            foreignKey: 'itemgroup_id',
            as: 'itemgroup'
        });
    };

    return TerritoryItemGroup;
};
