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

entity_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"driver",


"vehicle",


"agreement",


"invoice",


"payment",


"vehicle_expense",


"driver_expense",


"vendor",


"bill",


"ledger_entry",


"loan_schedule",


"vehicle_compliance",


"user"

        ],

      },

entity_ref: {
        type: DataTypes.TEXT,
      
      

      },

file_name: {
        type: DataTypes.TEXT,
      
      

      },

file_url: {
        type: DataTypes.TEXT,
      
      

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

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

  documents.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.documents.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.users, {
      as: 'uploaded_by_user',
      foreignKey: {
        name: 'uploaded_by_userId',
      },
      constraints: false,
    });




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

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



  return documents;
};


