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

course_name: {
        type: DataTypes.TEXT,
      
      

      },

course_code: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

delivery_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"read_and_understand",


"classroom",


"webinar",


"elearning",


"on_the_job"

        ],

      },

duration_hours: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  training_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.training_courses.hasMany(db.training_assignments, {
      as: 'training_assignments_course',
      foreignKey: {
          name: 'courseId',
      },
      constraints: false,
    });


















//end loop





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


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

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



  return training_courses;
};


