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 esignature_events = sequelize.define(
    'esignature_events',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

action: {
        type: DataTypes.ENUM,
      
      

        values: [

"sign",


"approve",


"reject",


"acknowledge"

        ],

      },

subject_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"document",


"document_version",


"change_request",


"training_assignment",


"record_batch",


"risk_assessment",


"capa",


"nonconformance",


"audit"

        ],

      },

subject_reference: {
        type: DataTypes.TEXT,
      
      

      },

signed_at: {
        type: DataTypes.DATE,
      
      

      },

meaning_of_signature: {
        type: DataTypes.TEXT,
      
      

      },

authentication_method: {
        type: DataTypes.TEXT,
      
      

      },

ip_address: {
        type: DataTypes.TEXT,
      
      

      },

user_agent: {
        type: DataTypes.TEXT,
      
      

      },

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

  esignature_events.associate = (db) => {


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


































//end loop



    db.esignature_events.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });



    db.esignature_events.hasMany(db.file, {
      as: 'signature_artifacts',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.esignature_events.getTableName(),
        belongsToColumn: 'signature_artifacts',
      },
    });


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

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



  return esignature_events;
};


