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

title: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

level: {
        type: DataTypes.ENUM,
      
      

        values: [

"beginner",


"intermediate",


"advanced"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

tags: {
        type: DataTypes.TEXT,
      
      

      },

estimated_hours: {
        type: DataTypes.DECIMAL,
      
      

      },

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


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



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









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









//end loop



    db.courses.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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



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


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

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



  return courses;
};


