'use strict';

module.exports = function (sequelize, DataTypes) {
    const Speaker = sequelize.define('Speaker', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
        },
        name: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        description: {
            type: DataTypes.TEXT,
        },
        short_description: {
            type: DataTypes.STRING,
        },
        slogan: {
            type: DataTypes.STRING,
        },
        twitter: {
            type: DataTypes.STRING,
        },
        facebook: {
            type: DataTypes.STRING,
        },
        youtube: {
            type: DataTypes.STRING,
        },
        linkedin: {
            type: DataTypes.STRING,
        },
        instagram: {
            type: DataTypes.STRING,
        },
        web: {
            type: DataTypes.STRING,
        },
        state: {
            type: DataTypes.STRING,
            allowNull: false,
            defaultValue: 'draft'
        },
        typeId: {
            type: DataTypes.UUID,
            foreignKey: true,
        },
        featured: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
        }
    }, {
        tableName: 'speakers',
        freezeTableName: true,
        timestamps: true,

        defaultScope: {
            include: [{
                model: sequelize.models.SpeakerType,
                as: 'type'
            },
                /*                {
                                    model: sequelize.models.Value,
                                    as: 'values',
                                    required: false,
                                },                
                */
            ]
        },
    });

    Speaker.associate = function (models) {

        Speaker.Type = Speaker.belongsTo(models.SpeakerType, { foreignKey: 'typeId', as: "type" });
        Speaker.UserCreate = Speaker.belongsTo(models.User, { foreignKey: 'userId', as: "user" });
        Speaker.Values = Speaker.belongsToMany(models.Value, {
            through: models.SpeakerValue,
            as: 'values',
            foreignKey: 'speakerId'
        });

        Speaker.EventDetails = Speaker.hasMany(models.EventDetail, { foreignKey: 'speakerId', as: "eventdetails" });
        Speaker.Questions = Speaker.hasMany(models.EventQuestion, { foreignKey: 'speakerId', as: "questions", required: false, });

        //OJO antes de force comentar
        // OJO GENERA UN FOREIGN KEY Con eventos y habrá ID de otras entidades que no exitan en la tabla eventos, porque son post o speakers
        Speaker.Multimedias = Speaker.hasMany(models.Multimedia, {
            foreignKey: 'entityId',
            as: { singular: 'multimedia', plural: 'multimedias' },
        });

        Speaker.Comments = Speaker.hasMany(models.Comment, {
            foreignKey: 'entityId',
            as: { singular: 'comment', plural: 'comments' }
        });

    };

    Speaker.addScope('onlyPublished', {
        where: {
            state: 'publish'
        },
    });


    Speaker.addScope('featured', {
        where: {
            featured: true
        },
    });

    Speaker.addScope('last', {
        order: [
            ['createdAt', 'DESC']
        ]
    });

    Speaker.addScope('includeMultimedias', () => {
        return {
            include: [{
                model: sequelize.models.Multimedia,
                as: { singular: 'multimedia', plural: 'multimedias' },
                required: false,
                include: [{
                    model: sequelize.models.MultimediaFile,
                    as: "multimediaFile",
                    order: [['createdAt', 'DESC']], // <- esto no funciona por ahora en Sequelize                       
                }]
            },
            ]
        }
    });

    Speaker.addScope('includeValues', (valueList = undefined) => {
        const config = {
            model: sequelize.models.Value,
            as: 'values',
            attributes: ['id', 'name'],
            required: false,
        };
        if (valueList) {
            config.where = { id: valueList };
            config.required = true;
        }
        return {
            include: [config]
        };
    });

    Speaker.addScope('includeComments', () => {
        return {
            include: [{
                model: sequelize.models.Comment,
                as: 'comments',
                required: false,
                include: [{
                    model: sequelize.models.User,
                    as: 'user',
                }]
            }]
        }
    });


    return Speaker;
};