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,
      
      

      },

compare_at_price: {
        type: DataTypes.DECIMAL,
      
      

      },

stock_quantity: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  product_variants.associate = (db) => {

    db.product_variants.belongsToMany(db.variant_attributes, {
      as: 'attributes',
      foreignKey: {
        name: 'product_variants_attributesId',
      },
      constraints: false,
      through: 'product_variantsAttributesVariant_attributes',
    });

    db.product_variants.belongsToMany(db.variant_attributes, {
      as: 'attributes_filter',
      foreignKey: {
        name: 'product_variants_attributesId',
      },
      constraints: false,
      through: 'product_variantsAttributesVariant_attributes',
    });


/// 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.variant_attributes, {
      as: 'variant_attributes_variant',
      foreignKey: {
          name: 'variantId',
      },
      constraints: false,
    });







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


