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

name: {
        type: DataTypes.TEXT,
      
      

      },

last_viewed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  compare_lists.associate = (db) => {

    db.compare_lists.belongsToMany(db.compare_items, {
      as: 'items',
      foreignKey: {
        name: 'compare_lists_itemsId',
      },
      constraints: false,
      through: 'compare_listsItemsCompare_items',
    });

    db.compare_lists.belongsToMany(db.compare_items, {
      as: 'items_filter',
      foreignKey: {
        name: 'compare_lists_itemsId',
      },
      constraints: false,
      through: 'compare_listsItemsCompare_items',
    });


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
















    db.compare_lists.hasMany(db.compare_items, {
      as: 'compare_items_compare_list',
      foreignKey: {
          name: 'compare_listId',
      },
      constraints: false,
    });





























//end loop



    db.compare_lists.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });




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

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



  return compare_lists;
};


