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: [

"text",


"image",


"video",


"carousel",


"live_replay"

        ],

      },

caption: {
        type: DataTypes.TEXT,
      
      

      },

link_url: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"friends_only",


"group_only",


"private"

        ],

      },

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

      },

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

      },

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

      },

location_text: {
        type: DataTypes.TEXT,
      
      

      },

language_code: {
        type: DataTypes.TEXT,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

scheduled_for: {
        type: DataTypes.DATE,
      
      

      },

like_count: {
        type: DataTypes.INTEGER,
      
      

      },

comment_count: {
        type: DataTypes.INTEGER,
      
      

      },

share_count: {
        type: DataTypes.INTEGER,
      
      

      },

view_count: {
        type: DataTypes.INTEGER,
      
      

      },

      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.comments, {
      as: 'comments_post',
      foreignKey: {
          name: 'postId',
      },
      constraints: false,
    });


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








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





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



    db.posts.hasMany(db.live_streams, {
      as: 'live_streams_replay_post',
      foreignKey: {
          name: 'replay_postId',
      },
      constraints: false,
    });


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


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


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





//end loop



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



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

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


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

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



  return posts;
};


