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_title: {
        type: DataTypes.TEXT,
      
      

      },

handle: {
        type: DataTypes.TEXT,
      
      

      },

vendor: {
        type: DataTypes.TEXT,
      
      

      },

product_type: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"draft",


"archived"

        ],

      },

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

      },

current_sales_rank: {
        type: DataTypes.INTEGER,
      
      

      },

total_sales_count: {
        type: DataTypes.INTEGER,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

      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.product_variants, {
      as: 'product_variants_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.stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.products.belongsTo(db.collections, {
      as: 'collection',
      foreignKey: {
        name: 'collectionId',
      },
      constraints: false,
    });

    db.products.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      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;
};


