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

media_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"photo",


"video"

        ],

      },

caption: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

posted_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

      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.story_views, {
      as: 'story_views_story',
      foreignKey: {
          name: 'storyId',
      },
      constraints: false,
    });



    db.stories.hasMany(db.highlight_items, {
      as: 'highlight_items_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: 'image',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.stories.getTableName(),
        belongsToColumn: 'image',
      },
    });

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


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

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



  return stories;
};


