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

adjustment_number: {
        type: DataTypes.TEXT,
      
      

      },

reason: {
        type: DataTypes.ENUM,
      
      

        values: [

"damage",


"shrinkage",


"expiry",


"count_correction",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected",


"posted"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  stock_adjustments.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_adjustments.hasMany(db.stock_adjustment_lines, {
      as: 'stock_adjustment_lines_stock_adjustment',
      foreignKey: {
          name: 'stock_adjustmentId',
      },
      constraints: false,
    });









































//end loop



    db.stock_adjustments.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.stock_adjustments.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

    db.stock_adjustments.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

    db.stock_adjustments.belongsTo(db.users, {
      as: 'approved_by',
      foreignKey: {
        name: 'approved_byId',
      },
      constraints: false,
    });

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




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

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



  return stock_adjustments;
};


