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

classroom_name: {
        type: DataTypes.TEXT,
      
      

      },

section_name: {
        type: DataTypes.TEXT,
      
      

      },

invite_code: {
        type: DataTypes.TEXT,
      
      

      },

invite_code_expires_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

room_description: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  classrooms.associate = (db) => {


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









    db.classrooms.hasMany(db.classroom_memberships, {
      as: 'classroom_memberships_classroom',
      foreignKey: {
          name: 'classroomId',
      },
      constraints: false,
    });


    db.classrooms.hasMany(db.teaching_assignments, {
      as: 'teaching_assignments_classroom',
      foreignKey: {
          name: 'classroomId',
      },
      constraints: false,
    });


    db.classrooms.hasMany(db.homeworks, {
      as: 'homeworks_classroom',
      foreignKey: {
          name: 'classroomId',
      },
      constraints: false,
    });







    db.classrooms.hasMany(db.gradebooks, {
      as: 'gradebooks_classroom',
      foreignKey: {
          name: 'classroomId',
      },
      constraints: false,
    });





    db.classrooms.hasMany(db.announcements, {
      as: 'announcements_classroom',
      foreignKey: {
          name: 'classroomId',
      },
      constraints: false,
    });











//end loop



    db.classrooms.belongsTo(db.schools, {
      as: 'school',
      foreignKey: {
        name: 'schoolId',
      },
      constraints: false,
    });

    db.classrooms.belongsTo(db.grade_levels, {
      as: 'grade_level',
      foreignKey: {
        name: 'grade_levelId',
      },
      constraints: false,
    });

    db.classrooms.belongsTo(db.users, {
      as: 'adviser_teacher',
      foreignKey: {
        name: 'adviser_teacherId',
      },
      constraints: false,
    });




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

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



  return classrooms;
};


