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

title: {
        type: DataTypes.TEXT,
      
      

      },

version_label: {
        type: DataTypes.TEXT,
      
      

      },

template_source: {
        type: DataTypes.ENUM,
      
      

        values: [

"system",


"tenant",


"public_user"

        ],

      },

template_body: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

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

  document_templates.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_templates.hasMany(db.contract_folders, {
      as: 'contract_folders_document_template',
      foreignKey: {
          name: 'document_templateId',
      },
      constraints: false,
    });

























//end loop



    db.document_templates.belongsTo(db.document_types, {
      as: 'document_type',
      foreignKey: {
        name: 'document_typeId',
      },
      constraints: false,
    });

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

    db.document_templates.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });

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



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


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

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



  return document_templates;
};


