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,
      },

template_name: {
        type: DataTypes.TEXT,
      
      

      },

template_code: {
        type: DataTypes.TEXT,
      
      

      },

template_family: {
        type: DataTypes.ENUM,
      
      

        values: [

"sop",


"bmr",


"coa",


"doc",


"risk_file",


"other"

        ],

      },

purpose: {
        type: DataTypes.TEXT,
      
      

      },

template_body: {
        type: DataTypes.TEXT,
      
      

      },

format: {
        type: DataTypes.ENUM,
      
      

        values: [

"html",


"docx",


"xlsx",


"pdf",


"mixed"

        ],

      },

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) => {

    db.document_templates.belongsToMany(db.clauses, {
      as: 'mapped_clauses',
      foreignKey: {
        name: 'document_templates_mapped_clausesId',
      },
      constraints: false,
      through: 'document_templatesMapped_clausesClauses',
    });

    db.document_templates.belongsToMany(db.clauses, {
      as: 'mapped_clauses_filter',
      foreignKey: {
        name: 'document_templates_mapped_clausesId',
      },
      constraints: false,
      through: 'document_templatesMapped_clausesClauses',
    });


/// 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.documents, {
      as: 'documents_template',
      foreignKey: {
          name: 'templateId',
      },
      constraints: false,
    });


























//end loop



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



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


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

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



  return document_templates;
};


