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

version_no: {
        type: DataTypes.INTEGER,
      
      

      },

number: {
        type: DataTypes.TEXT,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  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.document_versions.hasMany(db.documents, {
      as: 'documents_current_version',
      foreignKey: {
          name: 'current_versionId',
      },
      constraints: false,
    });





























//end loop



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



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


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

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



  return document_versions;
};


