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

title: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"awaiting_payment",


"in_authentication",


"in_signing",


"changes_requested",


"revoked",


"expired",


"completed",


"blocked"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

deadline_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

service_warning: {
        type: DataTypes.TEXT,
      
      

      },

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

  contract_folders.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_folders.hasMany(db.contract_documents, {
      as: 'contract_documents_contract_folder',
      foreignKey: {
          name: 'contract_folderId',
      },
      constraints: false,
    });



    db.contract_folders.hasMany(db.contract_participants, {
      as: 'contract_participants_contract_folder',
      foreignKey: {
          name: 'contract_folderId',
      },
      constraints: false,
    });



    db.contract_folders.hasMany(db.payments, {
      as: 'payments_contract_folder',
      foreignKey: {
          name: 'contract_folderId',
      },
      constraints: false,
    });







    db.contract_folders.hasMany(db.notification_records, {
      as: 'notification_records_contract_folder',
      foreignKey: {
          name: 'contract_folderId',
      },
      constraints: false,
    });



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





    db.contract_folders.hasMany(db.support_tickets, {
      as: 'support_tickets_contract_folder',
      foreignKey: {
          name: 'contract_folderId',
      },
      constraints: false,
    });






    db.contract_folders.hasMany(db.audit_events, {
      as: 'audit_events_contract_folder',
      foreignKey: {
          name: 'contract_folderId',
      },
      constraints: false,
    });



//end loop



    db.contract_folders.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.contract_folders.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      constraints: false,
    });

    db.contract_folders.belongsTo(db.service_types, {
      as: 'service_type',
      foreignKey: {
        name: 'service_typeId',
      },
      constraints: false,
    });

    db.contract_folders.belongsTo(db.workflow_definitions, {
      as: 'workflow_definition',
      foreignKey: {
        name: 'workflow_definitionId',
      },
      constraints: false,
    });

    db.contract_folders.belongsTo(db.document_templates, {
      as: 'document_template',
      foreignKey: {
        name: 'document_templateId',
      },
      constraints: false,
    });

    db.contract_folders.belongsTo(db.locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });

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




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

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



  return contract_folders;
};


