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

      dob: {
        type: DataTypes.DATE,
      },

      gender: {
        type: DataTypes.ENUM,

        values: ['male', 'female', 'other'],
      },

      admission_date: {
        type: DataTypes.DATE,
      },

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

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

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

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

    //end loop

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

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

    db.students.belongsTo(db.parents, {
      as: 'parent',
      foreignKey: {
        name: 'parentId',
      },
      constraints: false,
    });

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

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

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

  return students;
};
