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

caption: {
        type: DataTypes.TEXT,
      
      

      },

audio_title: {
        type: DataTypes.TEXT,
      
      

      },

audio_artist: {
        type: DataTypes.TEXT,
      
      

      },

duration_seconds: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  reels.associate = (db) => {


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


















    db.reels.hasMany(db.reel_reactions, {
      as: 'reel_reactions_reel',
      foreignKey: {
          name: 'reelId',
      },
      constraints: false,
    });


    db.reels.hasMany(db.reel_comments, {
      as: 'reel_comments_reel',
      foreignKey: {
          name: 'reelId',
      },
      constraints: false,
    });











//end loop



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



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

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


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

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



  return reels;
};


