const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const surveys = sequelize.define(
    'surveys',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

survey_name: {
        type: DataTypes.TEXT,
      
      

      },

survey_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pulse",


"onboarding_feedback",


"training_feedback",


"exit_intent"

        ],

      },

introduction_text: {
        type: DataTypes.TEXT,
      
      

      },

is_anonymous: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"closed"

        ],

      },

opens_at: {
        type: DataTypes.DATE,
      
      

      },

closes_at: {
        type: DataTypes.DATE,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  surveys.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity









































    db.surveys.hasMany(db.survey_questions, {
      as: 'survey_questions_survey',
      foreignKey: {
          name: 'surveyId',
      },
      constraints: false,
    });


    db.surveys.hasMany(db.survey_responses, {
      as: 'survey_responses_survey',
      foreignKey: {
          name: 'surveyId',
      },
      constraints: false,
    });






//end loop



    db.surveys.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




    db.surveys.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.surveys.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return surveys;
};


