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

lesson_title: {
        type: DataTypes.TEXT,
      
      

      },

lesson_summary: {
        type: DataTypes.TEXT,
      
      

      },

content_format: {
        type: DataTypes.ENUM,
      
      

        values: [

"structured_json",


"markdown",


"html"

        ],

      },

content_body: {
        type: DataTypes.TEXT,
      
      

      },

order_index: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

      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.lesson_progress, {
      as: 'lesson_progress_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });


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















//end loop



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




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

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



  return lessons;
};


