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,
      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"requisition",


"tender_dossier",


"bid_evaluation_report",


"award_memo",


"contract",


"invoice",


"delivery_note",


"acceptance_certificate",


"compliance_file",


"other"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

record_type: {
        type: DataTypes.TEXT,
      
      

      },

record_key: {
        type: DataTypes.TEXT,
      
      

      },

version_number: {
        type: DataTypes.INTEGER,
      
      

      },

access_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"public_within_org",


"restricted",


"confidential"

        ],

      },

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.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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



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


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

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



  return documents;
};


