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,
      },

nis: {
        type: DataTypes.TEXT,
      
      

      },

nisn: {
        type: DataTypes.TEXT,
      
      

      },

birth_place: {
        type: DataTypes.TEXT,
      
      

      },

birth_date: {
        type: DataTypes.DATE,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"laki_laki",


"perempuan"

        ],

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

enrollment_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"aktif",


"nonaktif",


"lulus",


"pindah"

        ],

      },

      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.parent_student_links, {
      as: 'parent_student_links_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.exam_attempts, {
      as: 'exam_attempts_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });




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





//end loop



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

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

    db.students.belongsTo(db.classes, {
      as: 'current_class',
      foreignKey: {
        name: 'current_classId',
      },
      constraints: false,
    });




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

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



  return students;
};


