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

page_key: {
        type: DataTypes.ENUM,
      
      

        values: [

"home",


"about",


"coffee_philosophy",


"vision_mission_values",


"coffee_origins",


"quality_control",


"export_services",


"sustainability",


"contact",


"footer"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

meta_title: {
        type: DataTypes.TEXT,
      
      

      },

meta_description: {
        type: DataTypes.TEXT,
      
      

      },

meta_keywords: {
        type: DataTypes.TEXT,
      
      

      },

hero_heading: {
        type: DataTypes.TEXT,
      
      

      },

hero_subheading: {
        type: DataTypes.TEXT,
      
      

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  pages.associate = (db) => {


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






    db.pages.hasMany(db.cta_buttons, {
      as: 'cta_buttons_page',
      foreignKey: {
          name: 'pageId',
      },
      constraints: false,
    });


    db.pages.hasMany(db.feature_highlights, {
      as: 'feature_highlights_page',
      foreignKey: {
          name: 'pageId',
      },
      constraints: false,
    });


    db.pages.hasMany(db.galleries, {
      as: 'galleries_page',
      foreignKey: {
          name: 'pageId',
      },
      constraints: false,
    });










    db.pages.hasMany(db.testimonials, {
      as: 'testimonials_page',
      foreignKey: {
          name: 'pageId',
      },
      constraints: false,
    });







//end loop



    db.pages.belongsTo(db.users, {
      as: 'author',
      foreignKey: {
        name: 'authorId',
      },
      constraints: false,
    });



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


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

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



  return pages;
};


