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

"photo",


"video",


"carousel"

        ],

      },

caption: {
        type: DataTypes.TEXT,
      
      

      },

location_name: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

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


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


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




    db.posts.hasMany(db.post_hashtags, {
      as: 'post_hashtags_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: 'images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.posts.getTableName(),
        belongsToColumn: 'images',
      },
    });

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


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

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



  return posts;
};


