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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"unlisted",


"private"

        ],

      },

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

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

duration_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

view_count: {
        type: DataTypes.INTEGER,
      
      

      },

like_count: {
        type: DataTypes.INTEGER,
      
      

      },

comment_count: {
        type: DataTypes.INTEGER,
      
      

      },

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

  videos.associate = (db) => {


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




































//end loop



    db.videos.belongsTo(db.creator_profiles, {
      as: 'creator',
      foreignKey: {
        name: 'creatorId',
      },
      constraints: false,
    });

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



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

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


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

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



  return videos;
};


