'use strict';

module.exports = function (sequelize, DataTypes) {
    const MultimediaFile = sequelize.define('MultimediaFile', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
        },
        name: {
            type: DataTypes.STRING,
        },
        description: {
            type: DataTypes.STRING,
        },
        class: {
            type: DataTypes.STRING,
        },
        provider: {
            type: DataTypes.STRING,
        },     
        code: {
            type: DataTypes.STRING,
        },  
        url: {
            type: DataTypes.STRING,
        },
        userId: {
            type: DataTypes.UUID,
            foreignKey: true,
        },   
    }, {
        tableName: 'multimedia_files',
        freezeTableName: true,
        timestamps: true,
    });

    MultimediaFile.associate = function (models) {
        MultimediaFile.UserCreate = MultimediaFile.belongsTo(models.User, { foreignKey: 'userId' }); 
        MultimediaFile.Multimedias = MultimediaFile.hasMany(models.Multimedia, { foreignKey: 'multimediafileId', as: { singular: 'multimedia', plural: 'multimedias' } });        
    };

    MultimediaFile.addScope('includeMultimedias', () => {
        return {
            include: [{ 
                model: sequelize.models.Multimedia, 
                as: { 
                    singular: 'multimedia', 
                    plural: 'multimedias' 
                },
                required: false,
            }]
        }
    });

    return MultimediaFile;
};