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

story_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"image",


"video",


"text"

        ],

      },

text_overlay: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"friends_only",


"close_friends"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

view_count: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  stories.associate = (db) => {


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







    db.stories.hasMany(db.reactions, {
      as: 'reactions_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });





















//end loop



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



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

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


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

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



  return stories;
};


