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

name: {
        type: DataTypes.TEXT,
      
      

      },

course_code: {
        type: DataTypes.TEXT,
      
      

      },

course_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"archived"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

      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.course_enrollments, {
      as: 'course_enrollments_course',
      foreignKey: {
          name: 'courseId',
      },
      constraints: false,
    });


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



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















//end loop



    db.courses.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

    db.courses.belongsTo(db.rooms, {
      as: 'default_room',
      foreignKey: {
        name: 'default_roomId',
      },
      constraints: false,
    });




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

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



  return courses;
};


