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

revision_number: {
        type: DataTypes.INTEGER,
      
      

      },

action_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"sign",


"accept_change_and_resign",


"revoke",


"decline"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"completed",


"rejected"

        ],

      },

action_at: {
        type: DataTypes.DATE,
      
      

      },

signature_hash: {
        type: DataTypes.TEXT,
      
      

      },

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

  signing_actions.associate = (db) => {


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




































    db.signing_actions.hasMany(db.revocation_requests, {
      as: 'revocation_requests_signing_action',
      foreignKey: {
          name: 'signing_actionId',
      },
      constraints: false,
    });









//end loop



    db.signing_actions.belongsTo(db.contract_folders, {
      as: 'contract_folder',
      foreignKey: {
        name: 'contract_folderId',
      },
      constraints: false,
    });

    db.signing_actions.belongsTo(db.contract_participants, {
      as: 'contract_participant',
      foreignKey: {
        name: 'contract_participantId',
      },
      constraints: false,
    });

    db.signing_actions.belongsTo(db.contract_documents, {
      as: 'contract_document',
      foreignKey: {
        name: 'contract_documentId',
      },
      constraints: false,
    });

    db.signing_actions.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return signing_actions;
};


