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

name: {
        type: DataTypes.TEXT,
      
      

      },

capacity: {
        type: DataTypes.INTEGER,
      
      

      },

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

  class_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.class_sections.hasMany(db.enrollments, {
      as: 'enrollments_class_section',
      foreignKey: {
          name: 'class_sectionId',
      },
      constraints: false,
    });










    db.class_sections.hasMany(db.attendance_sessions, {
      as: 'attendance_sessions_class_section',
      foreignKey: {
          name: 'class_sectionId',
      },
      constraints: false,
    });



    db.class_sections.hasMany(db.class_schedules, {
      as: 'class_schedules_class_section',
      foreignKey: {
          name: 'class_sectionId',
      },
      constraints: false,
    });


    db.class_sections.hasMany(db.assignments, {
      as: 'assignments_class_section',
      foreignKey: {
          name: 'class_sectionId',
      },
      constraints: false,
    });



    db.class_sections.hasMany(db.lesson_plans, {
      as: 'lesson_plans_class_section',
      foreignKey: {
          name: 'class_sectionId',
      },
      constraints: false,
    });










//end loop



    db.class_sections.belongsTo(db.school_years, {
      as: 'school_year',
      foreignKey: {
        name: 'school_yearId',
      },
      constraints: false,
    });

    db.class_sections.belongsTo(db.grades, {
      as: 'grade',
      foreignKey: {
        name: 'gradeId',
      },
      constraints: false,
    });

    db.class_sections.belongsTo(db.streams, {
      as: 'stream',
      foreignKey: {
        name: 'streamId',
      },
      constraints: false,
    });

    db.class_sections.belongsTo(db.staff_members, {
      as: 'homeroom_teacher',
      foreignKey: {
        name: 'homeroom_teacherId',
      },
      constraints: false,
    });




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

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



  return class_sections;
};


