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

document_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"photo",


"document",


"agreement",


"authorization_form",


"receipt",


"other"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

uploaded_at: {
        type: DataTypes.DATE,
      
      

      },

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

  request_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.request_documents.belongsTo(db.service_requests, {
      as: 'service_request',
      foreignKey: {
        name: 'service_requestId',
      },
      constraints: false,
    });

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

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



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

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


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

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



  return request_documents;
};


