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

title: {
        type: DataTypes.TEXT,
      
      

      },

section_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"hero",


"category_grid",


"featured_products",


"banner",


"rich_text"

        ],

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

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

  homepage_sections.associate = (db) => {

    db.homepage_sections.belongsToMany(db.products, {
      as: 'featured_products',
      foreignKey: {
        name: 'homepage_sections_featured_productsId',
      },
      constraints: false,
      through: 'homepage_sectionsFeatured_productsProducts',
    });

    db.homepage_sections.belongsToMany(db.products, {
      as: 'featured_products_filter',
      foreignKey: {
        name: 'homepage_sections_featured_productsId',
      },
      constraints: false,
      through: 'homepage_sectionsFeatured_productsProducts',
    });

    db.homepage_sections.belongsToMany(db.categories, {
      as: 'featured_categories',
      foreignKey: {
        name: 'homepage_sections_featured_categoriesId',
      },
      constraints: false,
      through: 'homepage_sectionsFeatured_categoriesCategories',
    });

    db.homepage_sections.belongsToMany(db.categories, {
      as: 'featured_categories_filter',
      foreignKey: {
        name: 'homepage_sections_featured_categoriesId',
      },
      constraints: false,
      through: 'homepage_sectionsFeatured_categoriesCategories',
    });


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




























//end loop





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


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

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



  return homepage_sections;
};


