'use strict';

module.exports = function (sequelize, DataTypes) {
    const Multimedia = sequelize.define('Multimedia', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
        },
        multimediafileId: {
            type: DataTypes.UUID,
            allowNull: false,
        },
        entityId: {
            type: DataTypes.UUID,
            allowNull: false,
        },
        entityName: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        type: {
            type: DataTypes.STRING,
            allowNull: false,
        },
    }, {
        indexes: [{
            unique: false,
            fields: ['entityId']
        }],
        tableName: 'multimedias',
        freezeTableName: true,
        timestamps: true,
    });

    Multimedia.addScope('includeMultimediaFile', () => {
        return {
            include: [
                { model: sequelize.models.MultimediaFile, as: 'multimediaFile' }
            ]
        }
    });

    Multimedia.associate = function (models) {
        Multimedia.MultimediaFile = Multimedia.belongsTo(models.MultimediaFile, { foreignKey: 'multimediafileId', as: "multimediaFile" });
    };

    return Multimedia;
};