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

full_name: {
        type: DataTypes.TEXT,
      
      

      },

student_code: {
        type: DataTypes.TEXT,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"female",


"male"

        ],

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

guardian_name: {
        type: DataTypes.TEXT,
      
      

      },

guardian_phone: {
        type: DataTypes.TEXT,
      
      

      },

address_text: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  students.associate = (db) => {


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















    db.students.hasMany(db.enrollments, {
      as: 'enrollments_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });









    db.students.hasMany(db.exam_results, {
      as: 'exam_results_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });



    db.students.hasMany(db.attendance_records, {
      as: 'attendance_records_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });




    db.students.hasMany(db.assignment_submissions, {
      as: 'assignment_submissions_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });









    db.students.hasMany(db.top_student_features, {
      as: 'top_student_features_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });



//end loop



    db.students.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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

    db.students.belongsTo(db.streams, {
      as: 'current_stream',
      foreignKey: {
        name: 'current_streamId',
      },
      constraints: false,
    });




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

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



  return students;
};


