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

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"purchase_requisition",


"purchase_order",


"goods_receipt",


"vendor_invoice",


"payment"

        ],

      },

source_reference: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  document_links.associate = (db) => {


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
































//end loop



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

    db.document_links.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return document_links;
};


