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,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"procedure",


"record",


"form",


"template",


"report",


"other"

        ],

      },

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_templates, {
      as: 'document_templates_document_type',
      foreignKey: {
          name: 'document_typeId',
      },
      constraints: false,
    });


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


























//end loop






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

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



  return document_types;
};


