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

grade_number: {
        type: DataTypes.INTEGER,
      
      

      },

level: {
        type: DataTypes.ENUM,
      
      

        values: [

"high_school",


"preparatory"

        ],

      },

label: {
        type: DataTypes.TEXT,
      
      

      },

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

  grades.associate = (db) => {


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











    db.grades.hasMany(db.subject_offerings, {
      as: 'subject_offerings_grade',
      foreignKey: {
          name: 'gradeId',
      },
      constraints: false,
    });



    db.grades.hasMany(db.students, {
      as: 'students_current_grade',
      foreignKey: {
          name: 'current_gradeId',
      },
      constraints: false,
    });


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








    db.grades.hasMany(db.study_materials, {
      as: 'study_materials_grade',
      foreignKey: {
          name: 'gradeId',
      },
      constraints: false,
    });


    db.grades.hasMany(db.exams, {
      as: 'exams_grade',
      foreignKey: {
          name: 'gradeId',
      },
      constraints: false,
    });










    db.grades.hasMany(db.admission_applications, {
      as: 'admission_applications_requested_grade',
      foreignKey: {
          name: 'requested_gradeId',
      },
      constraints: false,
    });








//end loop






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

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



  return grades;
};


