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

sku: {
        type: DataTypes.TEXT,
      
      

      },

product_name: {
        type: DataTypes.TEXT,
      
      

      },

brand: {
        type: DataTypes.TEXT,
      
      

      },

short_description: {
        type: DataTypes.TEXT,
      
      

      },

long_description: {
        type: DataTypes.TEXT,
      
      

      },

pack_size: {
        type: DataTypes.TEXT,
      
      

      },

units_per_case: {
        type: DataTypes.INTEGER,
      
      

      },

unit_weight_lbs: {
        type: DataTypes.DECIMAL,
      
      

      },

case_weight_lbs: {
        type: DataTypes.DECIMAL,
      
      

      },

uom: {
        type: DataTypes.TEXT,
      
      

      },

moq_cases: {
        type: DataTypes.INTEGER,
      
      

      },

temperature_zone: {
        type: DataTypes.ENUM,
      
      

        values: [

"ambient",


"refrigerated",


"frozen"

        ],

      },

product_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"discontinued",


"seasonal",


"out_of_stock"

        ],

      },

allergens: {
        type: DataTypes.TEXT,
      
      

      },

certifications: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  products.associate = (db) => {


/// 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.inventory_items, {
      as: 'inventory_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.price_list_items, {
      as: 'price_list_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.quote_items, {
      as: 'quote_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


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



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



//end loop



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



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

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


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

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



  return products;
};


