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

transfer_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"dispatched",


"in_transit",


"received",


"cancelled"

        ],

      },

dispatched_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,
    },
  );

  interbranch_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.interbranch_transfers.hasMany(db.transfer_lines, {
      as: 'transfer_lines_transfer',
      foreignKey: {
          name: 'transferId',
      },
      constraints: false,
    });







































//end loop



    db.interbranch_transfers.belongsTo(db.branches, {
      as: 'from_branch',
      foreignKey: {
        name: 'from_branchId',
      },
      constraints: false,
    });

    db.interbranch_transfers.belongsTo(db.branches, {
      as: 'to_branch',
      foreignKey: {
        name: 'to_branchId',
      },
      constraints: false,
    });

    db.interbranch_transfers.belongsTo(db.warehouses, {
      as: 'from_warehouse',
      foreignKey: {
        name: 'from_warehouseId',
      },
      constraints: false,
    });

    db.interbranch_transfers.belongsTo(db.warehouses, {
      as: 'to_warehouse',
      foreignKey: {
        name: 'to_warehouseId',
      },
      constraints: false,
    });

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

    db.interbranch_transfers.belongsTo(db.users, {
      as: 'received_by',
      foreignKey: {
        name: 'received_byId',
      },
      constraints: false,
    });

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




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

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



  return interbranch_transfers;
};


