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

variant_name: {
        type: DataTypes.TEXT,
      
      

      },

variant_sku: {
        type: DataTypes.TEXT,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

stock_quantity: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

option_summary: {
        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,
    });





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




    db.product_variants.hasMany(db.wishlist_items, {
      as: 'wishlist_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.hasMany(db.file, {
      as: 'variant_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.product_variants.getTableName(),
        belongsToColumn: 'variant_images',
      },
    });


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

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



  return product_variants;
};


