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

grn_number: {
        type: DataTypes.TEXT,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

quality_check: {
        type: DataTypes.ENUM,
      
      

        values: [

"pass",


"partial",


"fail"

        ],

      },

observations: {
        type: DataTypes.TEXT,
      
      

      },

receiver_signature: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"submitted",


"discrepancy_reported",


"accepted",


"rejected"

        ],

      },

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

  goods_receipt_notes.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_receipt_notes.hasMany(db.grn_lines, {
      as: 'grn_lines_goods_receipt_note',
      foreignKey: {
          name: 'goods_receipt_noteId',
      },
      constraints: false,
    });



    db.goods_receipt_notes.hasMany(db.discrepancy_cases, {
      as: 'discrepancy_cases_goods_receipt_note',
      foreignKey: {
          name: 'goods_receipt_noteId',
      },
      constraints: false,
    });









//end loop



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

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

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

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



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


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

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



  return goods_receipt_notes;
};


