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

name_ar: {
        type: DataTypes.TEXT,
      
      

      },

name_en: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

description_ar: {
        type: DataTypes.TEXT,
      
      

      },

description_en: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

  collections.associate = (db) => {

    db.collections.belongsToMany(db.products, {
      as: 'products',
      foreignKey: {
        name: 'collections_productsId',
      },
      constraints: false,
      through: 'collectionsProductsProducts',
    });

    db.collections.belongsToMany(db.products, {
      as: 'products_filter',
      foreignKey: {
        name: 'collections_productsId',
      },
      constraints: false,
      through: 'collectionsProductsProducts',
    });


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












































//end loop





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


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

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



  return collections;
};


