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

student_number: {
        type: DataTypes.TEXT,
      
      

      },

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"male",


"female",


"other",


"prefer_not_to_say"

        ],

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

enrollment_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"prospect",


"enrolled",


"inactive",


"graduated",


"transferred"

        ],

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

      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.guardians, {
      as: 'guardians_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });




    db.students.hasMany(db.class_enrollments, {
      as: 'class_enrollments_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.invoices, {
      as: 'invoices_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });




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






//end loop



    db.students.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.students.belongsTo(db.campuses, {
      as: 'campus',
      foreignKey: {
        name: 'campusId',
      },
      constraints: false,
    });



    db.students.hasMany(db.file, {
      as: 'photo',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.students.getTableName(),
        belongsToColumn: 'photo',
      },
    });


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

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



  return students;
};


