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

name_ar: {
        type: DataTypes.TEXT,
      
      

      },

name_en: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

short_description_ar: {
        type: DataTypes.TEXT,
      
      

      },

short_description_en: {
        type: DataTypes.TEXT,
      
      

      },

description_ar: {
        type: DataTypes.TEXT,
      
      

      },

description_en: {
        type: DataTypes.TEXT,
      
      

      },

sku: {
        type: DataTypes.TEXT,
      
      

      },

barcode: {
        type: DataTypes.TEXT,
      
      

      },

price_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

compare_at_price_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

cost_iqd: {
        type: DataTypes.DECIMAL,
      
      

      },

stock_on_hand: {
        type: DataTypes.INTEGER,
      
      

      },

low_stock_threshold: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

      },

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

      },

condition: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"used",


"refurbished"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

      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.collections, {
      as: 'collections',
      foreignKey: {
        name: 'products_collectionsId',
      },
      constraints: false,
      through: 'productsCollectionsCollections',
    });

    db.products.belongsToMany(db.collections, {
      as: 'collections_filter',
      foreignKey: {
        name: 'products_collectionsId',
      },
      constraints: false,
      through: 'productsCollectionsCollections',
    });

    db.products.belongsToMany(db.frequently_bought_together_items, {
      as: 'fbt_items',
      foreignKey: {
        name: 'products_fbt_itemsId',
      },
      constraints: false,
      through: 'productsFbt_itemsFrequently_bought_together_items',
    });

    db.products.belongsToMany(db.frequently_bought_together_items, {
      as: 'fbt_items_filter',
      foreignKey: {
        name: 'products_fbt_itemsId',
      },
      constraints: false,
      through: 'productsFbt_itemsFrequently_bought_together_items',
    });

    db.products.belongsToMany(db.seo_entries, {
      as: 'seo_entries',
      foreignKey: {
        name: 'products_seo_entriesId',
      },
      constraints: false,
      through: 'productsSeo_entriesSeo_entries',
    });

    db.products.belongsToMany(db.seo_entries, {
      as: 'seo_entries_filter',
      foreignKey: {
        name: 'products_seo_entriesId',
      },
      constraints: false,
      through: 'productsSeo_entriesSeo_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.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.compare_items, {
      as: 'compare_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,
    });









    db.products.hasMany(db.frequently_bought_together_groups, {
      as: 'frequently_bought_together_groups_primary_product',
      foreignKey: {
          name: 'primary_productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.frequently_bought_together_items, {
      as: 'frequently_bought_together_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;
};


