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,
      },

title: {
        type: DataTypes.TEXT,
      
      

      },

url: {
        type: DataTypes.TEXT,
      
      

      },

detected_on: {
        type: DataTypes.DATE,
      
      

      },

form_schema: {
        type: DataTypes.TEXT,
      
      

      },

page_context: {
        type: DataTypes.ENUM,
      
      

        values: [

"generic",


"multiple_choice",


"scale",


"open_ended"

        ],

      },

language: {
        type: DataTypes.ENUM,
      
      

        values: [

"arabic",


"english",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"analyzed",


"filled"

        ],

      },

      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.fill_sessions, {
      as: 'fill_sessions_survey',
      foreignKey: {
          name: 'surveyId',
      },
      constraints: false,
    });







//end loop



    db.surveys.belongsTo(db.extensions, {
      as: 'extension',
      foreignKey: {
        name: 'extensionId',
      },
      constraints: false,
    });




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

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



  return surveys;
};


