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

module.exports = (sequelize) => {
    const UserProject = sequelize.define('userproject', {
        user_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'users',
                key: 'id',
                primaryKey: true
            }
        },
        project_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'projects',
                key: 'id',
                primaryKey: true
            }
        }
    }, {
        tableName: 'userprojects',
    }, {
        freezeTableName: true
    });

    UserProject.associate = (models) => {
        /**
         * Associate UserProject with User
         * @param {object} models - The models to associate with.
         * @property {object} user - The User model associated with UserProject.
         */
        UserProject.belongsTo(models.User, {
            foreignKey: 'user_id',
            as: 'user'
        });
        /**
         * Associate UserProject with Project
         * @param {object} models - The models to associate with.
         * @property {object} project - The Project model associated with UserProject.
         */
        UserProject.belongsTo(models.Project, {
            foreignKey: 'project_id',
            as: 'project'
        });
    };

    return UserProject;
};