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

document_title: {
        type: DataTypes.TEXT,
      
      

      },

document_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"final",


"exported"

        ],

      },

format: {
        type: DataTypes.ENUM,
      
      

        values: [

"canvas",


"docx",


"pdf",


"txt"

        ],

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

last_edited_at: {
        type: DataTypes.DATE,
      
      

      },

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

  generated_documents.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

















    db.generated_documents.hasMany(db.document_versions, {
      as: 'document_versions_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });















//end loop



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

    db.generated_documents.belongsTo(db.matters, {
      as: 'matter',
      foreignKey: {
        name: 'matterId',
      },
      constraints: false,
    });

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

    db.generated_documents.belongsTo(db.files, {
      as: 'export_file',
      foreignKey: {
        name: 'export_fileId',
      },
      constraints: false,
    });




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

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



  return generated_documents;
};


