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

title: {
        type: DataTypes.TEXT,
      
      

      },

document_source: {
        type: DataTypes.ENUM,
      
      

        values: [

"from_template",


"uploaded",


"generated"

        ],

      },

content_hash: {
        type: DataTypes.TEXT,
      
      

      },

current_revision_number: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  contract_documents.associate = (db) => {


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






















    db.contract_documents.hasMany(db.contract_data_fields, {
      as: 'contract_data_fields_contract_document',
      foreignKey: {
          name: 'contract_documentId',
      },
      constraints: false,
    });












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


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






    db.contract_documents.hasMany(db.ai_contract_classifications, {
      as: 'ai_contract_classifications_contract_document',
      foreignKey: {
          name: 'contract_documentId',
      },
      constraints: false,
    });






//end loop



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

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



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

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

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


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

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



  return contract_documents;
};


