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

product_name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

short_description: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

compare_at_price: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

stock_quantity: {
        type: DataTypes.INTEGER,
      
      

      },

sku: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"archived"

        ],

      },

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

      },

discount_percent: {
        type: DataTypes.INTEGER,
      
      

      },

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

  products.associate = (db) => {

    db.products.belongsToMany(db.product_variants, {
      as: 'variants',
      foreignKey: {
        name: 'products_variantsId',
      },
      constraints: false,
      through: 'productsVariantsProduct_variants',
    });

    db.products.belongsToMany(db.product_variants, {
      as: 'variants_filter',
      foreignKey: {
        name: 'products_variantsId',
      },
      constraints: false,
      through: 'productsVariantsProduct_variants',
    });

    db.products.belongsToMany(db.product_reviews, {
      as: 'reviews',
      foreignKey: {
        name: 'products_reviewsId',
      },
      constraints: false,
      through: 'productsReviewsProduct_reviews',
    });

    db.products.belongsToMany(db.product_reviews, {
      as: 'reviews_filter',
      foreignKey: {
        name: 'products_reviewsId',
      },
      constraints: false,
      through: 'productsReviewsProduct_reviews',
    });

    db.products.belongsToMany(db.seo_entries, {
      as: 'seo',
      foreignKey: {
        name: 'products_seoId',
      },
      constraints: false,
      through: 'productsSeoSeo_entries',
    });

    db.products.belongsToMany(db.seo_entries, {
      as: 'seo_filter',
      foreignKey: {
        name: 'products_seoId',
      },
      constraints: false,
      through: 'productsSeoSeo_entries',
    });


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













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



    db.products.hasMany(db.seo_entries, {
      as: 'seo_entries_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.product_reviews, {
      as: 'product_reviews_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.wishlist_items, {
      as: 'wishlist_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.comparison_items, {
      as: 'comparison_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.cart_items, {
      as: 'cart_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });







    db.products.hasMany(db.order_items, {
      as: 'order_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });

















//end loop



    db.products.belongsTo(db.categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

    db.products.belongsTo(db.brands, {
      as: 'brand',
      foreignKey: {
        name: 'brandId',
      },
      constraints: false,
    });

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



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


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

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



  return products;
};


