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

title: {
        type: DataTypes.TEXT,
      
      

      },

order_index: {
        type: DataTypes.INTEGER,
      
      

      },

content_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"article",


"video",


"quiz",


"interactive",


"assignment"

        ],

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

video_url: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

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

  lessons.associate = (db) => {


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











    db.lessons.hasMany(db.content_items, {
      as: 'content_items_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });














    db.lessons.hasMany(db.comments, {
      as: 'comments_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });









//end loop



    db.lessons.belongsTo(db.courses, {
      as: 'course',
      foreignKey: {
        name: 'courseId',
      },
      constraints: false,
    });

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



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


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

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



  return lessons;
};


