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

name: {
        type: DataTypes.TEXT,
      
      

      },

sku: {
        type: DataTypes.TEXT,
      
      

      },

price_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

stock_on_hand: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

attributes_json: {
        type: DataTypes.TEXT,
      
      

      },

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

  product_variants.associate = (db) => {


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


















    db.product_variants.hasMany(db.cart_items, {
      as: 'cart_items_variant',
      foreignKey: {
          name: 'variantId',
      },
      constraints: false,
    });



    db.product_variants.hasMany(db.order_items, {
      as: 'order_items_variant',
      foreignKey: {
          name: 'variantId',
      },
      constraints: false,
    });

























//end loop



    db.product_variants.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });




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

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



  return product_variants;
};


