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,
      },

grn_number: {
        type: DataTypes.TEXT,
      
      

      },

receipt_date: {
        type: DataTypes.DATE,
      
      

      },

posted_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"posted",


"cancelled"

        ],

      },

remarks: {
        type: DataTypes.TEXT,
      
      

      },

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

  goods_receipts.associate = (db) => {


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


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














//end loop



    db.goods_receipts.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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

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

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

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



    db.goods_receipts.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.goods_receipts.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return goods_receipts;
};


