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

reception_number: {
        type: DataTypes.TEXT,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"validated",


"canceled"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  stock_receptions.associate = (db) => {


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






























    db.stock_receptions.hasMany(db.stock_reception_items, {
      as: 'stock_reception_items_stock_reception',
      foreignKey: {
          name: 'stock_receptionId',
      },
      constraints: false,
    });









//end loop



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

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

    db.stock_receptions.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

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



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


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

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



  return stock_receptions;
};


