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

gr_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"posted",


"voided"

        ],

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

posted_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  goods_receipts.associate = (db) => {

    db.goods_receipts.belongsToMany(db.goods_receipt_lines, {
      as: 'goods_receipt_lines',
      foreignKey: {
        name: 'goods_receipts_goods_receipt_linesId',
      },
      constraints: false,
      through: 'goods_receiptsGoods_receipt_linesGoods_receipt_lines',
    });

    db.goods_receipts.belongsToMany(db.goods_receipt_lines, {
      as: 'goods_receipt_lines_filter',
      foreignKey: {
        name: 'goods_receipts_goods_receipt_linesId',
      },
      constraints: false,
      through: 'goods_receiptsGoods_receipt_linesGoods_receipt_lines',
    });


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


























    db.goods_receipts.hasMany(db.goods_receipt_lines, {
      as: 'goods_receipt_lines_goods_receipt',
      foreignKey: {
          name: 'goods_receiptId',
      },
      constraints: false,
    });








//end loop



    db.goods_receipts.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.goods_receipts.belongsTo(db.purchase_orders, {
      as: 'purchase_order',
      foreignKey: {
        name: 'purchase_orderId',
      },
      constraints: false,
    });

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




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

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



  return goods_receipts;
};


