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

module.exports = (sequelize) => {
    const EmployeeTerritory = sequelize.define('employeeterritorie', {
        employee_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'employees',
                key: 'id',
                primaryKey: true
            }
        },
        territory_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'territories',
                key: 'id',
                primaryKey: true
            }
        }
    }, {
        tableName: 'employeeterritories',
    }, {
        freezeTableName: true
    });

    /**
     * Associates the EmployeeTerritory model with other models.
     * @param {object} models - The models to associate with.
     */
    EmployeeTerritory.associate = (models) => {
        /**
         * Associate EmployeeTerritory with Employee
         * @param {object} models - The models to associate with.
         * @property {object} employee - The Employee model associated with EmployeeTerritory.
         */
        EmployeeTerritory.belongsTo(models.Employee, {
            foreignKey: 'employee_id',
            as: 'employee'
        });

        /**
         * Associate EmployeeTerritory with Territory
         * @param {object} models - The models to associate with.
         * @property {object} territory - The Territory model associated with EmployeeTerritory.
         */
        EmployeeTerritory.belongsTo(models.Territory, {
            foreignKey: 'territory_id',
            as: 'territory'
        });
    };

    return EmployeeTerritory;
};
