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

post_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"short_video",


"live_stream",


"text",


"image_gallery"

        ],

      },

caption: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"followers",


"private"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"removed"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  posts.associate = (db) => {


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

























    db.posts.hasMany(db.post_engagements, {
      as: 'post_engagements_post',
      foreignKey: {
          name: 'postId',
      },
      constraints: false,
    });




















//end loop



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

    db.posts.belongsTo(db.deep_dive_links, {
      as: 'deep_dive_link',
      foreignKey: {
        name: 'deep_dive_linkId',
      },
      constraints: false,
    });

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



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

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


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

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



  return posts;
};


