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

revision_number: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"proposed",


"approved",


"rejected",


"superseded"

        ],

      },

proposed_at: {
        type: DataTypes.DATE,
      
      

      },

change_summary: {
        type: DataTypes.TEXT,
      
      

      },

content_hash: {
        type: DataTypes.TEXT,
      
      

      },

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

  document_revisions.associate = (db) => {


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



































    db.document_revisions.hasMany(db.revision_approvals, {
      as: 'revision_approvals_document_revision',
      foreignKey: {
          name: 'document_revisionId',
      },
      constraints: false,
    });










//end loop



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

    db.document_revisions.belongsTo(db.contract_participants, {
      as: 'proposed_by_participant',
      foreignKey: {
        name: 'proposed_by_participantId',
      },
      constraints: false,
    });

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



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


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

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



  return document_revisions;
};


