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

course_title: {
        type: DataTypes.TEXT,
      
      

      },

course_description: {
        type: DataTypes.TEXT,
      
      

      },

level: {
        type: DataTypes.ENUM,
      
      

        values: [

"l1",


"l2",


"l3"

        ],

      },

access_tier: {
        type: DataTypes.ENUM,
      
      

        values: [

"free",


"member"

        ],

      },

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

      },

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

  courses.associate = (db) => {


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











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


    db.courses.hasMany(db.enrollments, {
      as: 'enrollments_course',
      foreignKey: {
          name: 'courseId',
      },
      constraints: false,
    });

















//end loop






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

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



  return courses;
};


