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

block_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"text",


"bulleted_list",


"numbered_list",


"table",


"toggle",


"image",


"pdf",


"link",


"code"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

text_content: {
        type: DataTypes.TEXT,
      
      

      },

code_language: {
        type: DataTypes.TEXT,
      
      

      },

link_url: {
        type: DataTypes.TEXT,
      
      

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  blocks.associate = (db) => {


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

































//end loop



    db.blocks.belongsTo(db.pages, {
      as: 'page',
      foreignKey: {
        name: 'pageId',
      },
      constraints: false,
    });

    db.blocks.belongsTo(db.blocks, {
      as: 'parent_block',
      foreignKey: {
        name: 'parent_blockId',
      },
      constraints: false,
    });

    db.blocks.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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

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


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

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



  return blocks;
};


