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,
      },

title: {
        type: DataTypes.TEXT,
      
      

      },

icon_emoji: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"private",


"workspace",


"public_link"

        ],

      },

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

      },

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

      },

last_edited_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.blocks, {
      as: 'blocks_page',
      foreignKey: {
          name: 'pageId',
      },
      constraints: false,
    });























//end loop



    db.pages.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.pages.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.pages.belongsTo(db.topics, {
      as: 'topic',
      foreignKey: {
        name: 'topicId',
      },
      constraints: false,
    });

    db.pages.belongsTo(db.pages, {
      as: 'parent_page',
      foreignKey: {
        name: 'parent_pageId',
      },
      constraints: false,
    });

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



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


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

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



  return pages;
};


