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

name: {
        type: DataTypes.TEXT,
      
      

      },

section_code: {
        type: DataTypes.TEXT,
      
      

      },

grading_scheme: {
        type: DataTypes.ENUM,
      
      

        values: [

"points",


"percentage",


"letter"

        ],

      },

pass_mark: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"active",


"completed",


"archived"

        ],

      },

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

  course_sections.associate = (db) => {


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



















    db.course_sections.hasMany(db.timetable_entries, {
      as: 'timetable_entries_course_section',
      foreignKey: {
          name: 'course_sectionId',
      },
      constraints: false,
    });


    db.course_sections.hasMany(db.attendance_sessions, {
      as: 'attendance_sessions_course_section',
      foreignKey: {
          name: 'course_sectionId',
      },
      constraints: false,
    });



    db.course_sections.hasMany(db.assignments, {
      as: 'assignments_course_section',
      foreignKey: {
          name: 'course_sectionId',
      },
      constraints: false,
    });



    db.course_sections.hasMany(db.grade_items, {
      as: 'grade_items_course_section',
      foreignKey: {
          name: 'course_sectionId',
      },
      constraints: false,
    });








//end loop



    db.course_sections.belongsTo(db.classes, {
      as: 'class',
      foreignKey: {
        name: 'classId',
      },
      constraints: false,
    });

    db.course_sections.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.course_sections.belongsTo(db.terms, {
      as: 'term',
      foreignKey: {
        name: 'termId',
      },
      constraints: false,
    });

    db.course_sections.belongsTo(db.teachers, {
      as: 'teacher',
      foreignKey: {
        name: 'teacherId',
      },
      constraints: false,
    });




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

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



  return course_sections;
};


