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

transfer_number: {
        type: DataTypes.TEXT,
      
      

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

shipped_at: {
        type: DataTypes.DATE,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"requested",


"approved",


"shipped",


"received",


"cancelled"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  stock_transfers.associate = (db) => {


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



























    db.stock_transfers.hasMany(db.stock_transfer_items, {
      as: 'stock_transfer_items_stock_transfer',
      foreignKey: {
          name: 'stock_transferId',
      },
      constraints: false,
    });



//end loop



    db.stock_transfers.belongsTo(db.stores, {
      as: 'from_store',
      foreignKey: {
        name: 'from_storeId',
      },
      constraints: false,
    });

    db.stock_transfers.belongsTo(db.stores, {
      as: 'to_store',
      foreignKey: {
        name: 'to_storeId',
      },
      constraints: false,
    });

    db.stock_transfers.belongsTo(db.users, {
      as: 'requested_by_user',
      foreignKey: {
        name: 'requested_by_userId',
      },
      constraints: false,
    });

    db.stock_transfers.belongsTo(db.users, {
      as: 'approved_by_user',
      foreignKey: {
        name: 'approved_by_userId',
      },
      constraints: false,
    });

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




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

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



  return stock_transfers;
};


