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

title: {
        type: DataTypes.TEXT,
      
      

      },

section_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"hero",


"services",


"projects",


"about",


"cta",


"testimonials",


"faq",


"contact",


"custom"

        ],

      },

subtitle: {
        type: DataTypes.TEXT,
      
      

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  home_sections.associate = (db) => {

    db.home_sections.belongsToMany(db.services, {
      as: 'featured_services',
      foreignKey: {
        name: 'home_sections_featured_servicesId',
      },
      constraints: false,
      through: 'home_sectionsFeatured_servicesServices',
    });

    db.home_sections.belongsToMany(db.services, {
      as: 'featured_services_filter',
      foreignKey: {
        name: 'home_sections_featured_servicesId',
      },
      constraints: false,
      through: 'home_sectionsFeatured_servicesServices',
    });

    db.home_sections.belongsToMany(db.projects, {
      as: 'featured_projects',
      foreignKey: {
        name: 'home_sections_featured_projectsId',
      },
      constraints: false,
      through: 'home_sectionsFeatured_projectsProjects',
    });

    db.home_sections.belongsToMany(db.projects, {
      as: 'featured_projects_filter',
      foreignKey: {
        name: 'home_sections_featured_projectsId',
      },
      constraints: false,
      through: 'home_sectionsFeatured_projectsProjects',
    });


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
















//end loop





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


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

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



  return home_sections;
};


