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

      exam_date: {
        type: DataTypes.DATE,
      },

      max_marks: {
        type: DataTypes.DECIMAL,
      },

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

  exam_subjects.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.exam_subjects.hasMany(db.marks, {
      as: 'marks_exam_subject',
      foreignKey: {
        name: 'exam_subjectId',
      },
      constraints: false,
    });

    //end loop

    db.exam_subjects.belongsTo(db.exams, {
      as: 'exam',
      foreignKey: {
        name: 'examId',
      },
      constraints: false,
    });

    db.exam_subjects.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

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

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

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

  return exam_subjects;
};
