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

transfer_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"requested",


"approved",


"picked",


"shipped",


"received",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

shipped_at: {
        type: DataTypes.DATE,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  inventory_transfers.associate = (db) => {

    db.inventory_transfers.belongsToMany(db.inventory_transfer_lines, {
      as: 'lines',
      foreignKey: {
        name: 'inventory_transfers_linesId',
      },
      constraints: false,
      through: 'inventory_transfersLinesInventory_transfer_lines',
    });

    db.inventory_transfers.belongsToMany(db.inventory_transfer_lines, {
      as: 'lines_filter',
      foreignKey: {
        name: 'inventory_transfers_linesId',
      },
      constraints: false,
      through: 'inventory_transfersLinesInventory_transfer_lines',
    });


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
























    db.inventory_transfers.hasMany(db.inventory_transfer_lines, {
      as: 'inventory_transfer_lines_transfer',
      foreignKey: {
          name: 'transferId',
      },
      constraints: false,
    });

































//end loop



    db.inventory_transfers.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

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

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

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




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

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



  return inventory_transfers;
};


