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

movement_at: {
        type: DataTypes.DATE,
      
      

      },

quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

source_type: {
        type: DataTypes.TEXT,
      
      

      },

source_ref: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

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

    db.stock_movements.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

    db.stock_movements.belongsTo(db.locations, {
      as: 'from_location',
      foreignKey: {
        name: 'from_locationId',
      },
      constraints: false,
    });

    db.stock_movements.belongsTo(db.locations, {
      as: 'to_location',
      foreignKey: {
        name: 'to_locationId',
      },
      constraints: false,
    });

    db.stock_movements.belongsTo(db.lots, {
      as: 'lot',
      foreignKey: {
        name: 'lotId',
      },
      constraints: false,
    });

    db.stock_movements.belongsTo(db.serial_numbers, {
      as: 'serial_number',
      foreignKey: {
        name: 'serial_numberId',
      },
      constraints: false,
    });

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




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

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



  return stock_movements;
};


