'use strict';
const moment = require('moment');
const Sequelize = require('sequelize');
moment.locale('es');

module.exports = function (sequelize, DataTypes) {
    const EventQuestion = sequelize.define('EventQuestion', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
        },
        question: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        answer: {
            type: DataTypes.TEXT,
        },
        anonimous: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
        },
        answered: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
        },
        discared: {
            type: DataTypes.BOOLEAN,
            defaultValue: false,
        },
    }, {
        tableName: 'events_questions',
        freezeTableName: true,
        timestamps: true,
    });

    EventQuestion.associate = function (models) {
        EventQuestion.Event = EventQuestion.belongsTo(models.Event, { foreignKey: 'eventId' });
        EventQuestion.Speaker = EventQuestion.belongsTo(models.Speaker, { foreignKey: 'speakerId' });         
        EventQuestion.User = EventQuestion.belongsTo(models.User, { foreignKey: 'userId' });
    };
  
    EventQuestion.addScope('includeUser', () => {
        return {
            include: [{
                model: sequelize.models.User,
            }],
        }
    });

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

    return EventQuestion;
};