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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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_types.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_types.hasMany(db.document_template_variables, {
      as: 'document_template_variables_document_type',
      foreignKey: {
          name: 'document_typeId',
      },
      constraints: false,
    });


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




























    db.document_types.hasMany(db.service_suitability_rules, {
      as: 'service_suitability_rules_document_type',
      foreignKey: {
          name: 'document_typeId',
      },
      constraints: false,
    });




//end loop



    db.document_types.belongsTo(db.document_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

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




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

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



  return document_types;
};


