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

resource_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"requisition",


"approval_step",


"procurement_case",


"tender_event",


"supplier",


"purchase_order",


"grn",


"san",


"discrepancy",


"award",


"offer"

        ],

      },

resource_reference: {
        type: DataTypes.TEXT,
      
      

      },

visibility_scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"role",


"project",


"department",


"restricted"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

keywords: {
        type: DataTypes.TEXT,
      
      

      },

amount_usd: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"archived",


"sealed"

        ],

      },

archived_at: {
        type: DataTypes.DATE,
      
      

      },

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

  archive_items.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.archive_items.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.archive_items.belongsTo(db.users, {
      as: 'archived_by',
      foreignKey: {
        name: 'archived_byId',
      },
      constraints: false,
    });

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



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


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

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



  return archive_items;
};


