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

item_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"creator",


"stream",


"video",


"product"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

subtitle: {
        type: DataTypes.TEXT,
      
      

      },

score: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"hidden"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  discovery_items.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.discovery_items.belongsTo(db.creator_profiles, {
      as: 'creator',
      foreignKey: {
        name: 'creatorId',
      },
      constraints: false,
    });

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



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


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

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



  return discovery_items;
};


