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

version_no: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

number: {
        type: DataTypes.TEXT,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  company_document_versions.associate = (db) => {


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











    db.company_document_versions.hasMany(db.company_documents, {
      as: 'company_documents_current_version',
      foreignKey: {
          name: 'current_versionId',
      },
      constraints: false,
    });





































//end loop



    db.company_document_versions.belongsTo(db.company_documents, {
      as: 'document',
      foreignKey: {
        name: 'documentId',
      },
      constraints: false,
    });



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


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

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



  return company_document_versions;
};


