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

current_price: {
        type: DataTypes.DECIMAL,
      
      

      },

pricing_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"fixed",


"dynamic",


"promotion"

        ],

      },

reorder_point: {
        type: DataTypes.INTEGER,
      
      

      },

target_stock: {
        type: DataTypes.INTEGER,
      
      

      },

is_listed: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

tax_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  store_products.associate = (db) => {


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
























    db.store_products.hasMany(db.store_inventory_batches, {
      as: 'store_inventory_batches_store_product',
      foreignKey: {
          name: 'store_productId',
      },
      constraints: false,
    });






    db.store_products.hasMany(db.shelf_slots, {
      as: 'shelf_slots_store_product',
      foreignKey: {
          name: 'store_productId',
      },
      constraints: false,
    });




    db.store_products.hasMany(db.sales_transaction_lines, {
      as: 'sales_transaction_lines_store_product',
      foreignKey: {
          name: 'store_productId',
      },
      constraints: false,
    });




















//end loop



    db.store_products.belongsTo(db.market_stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.store_products.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });




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

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



  return store_products;
};


