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

template_name: {
        type: DataTypes.TEXT,
      
      

      },

template_category: {
        type: DataTypes.ENUM,
      
      

        values: [

"motion",


"pleading",


"brief",


"letter",


"contract",


"memo",


"form",


"other"

        ],

      },

jurisdiction: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

template_body: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  legal_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.legal_templates.hasMany(db.template_fields, {
      as: 'template_fields_template',
      foreignKey: {
          name: 'templateId',
      },
      constraints: false,
    });


    db.legal_templates.hasMany(db.generated_documents, {
      as: 'generated_documents_template',
      foreignKey: {
          name: 'templateId',
      },
      constraints: false,
    });
















//end loop



    db.legal_templates.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

    db.legal_templates.belongsTo(db.files, {
      as: 'source_file',
      foreignKey: {
        name: 'source_fileId',
      },
      constraints: false,
    });




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

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



  return legal_templates;
};


