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

started_at: {
        type: DataTypes.DATE,
      
      

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"belum_mulai",


"sedang",


"selesai",


"dibatalkan"

        ],

      },

score_total: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  exam_attempts.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_attempts.hasMany(db.exam_answers, {
      as: 'exam_answers_exam_attempt',
      foreignKey: {
          name: 'exam_attemptId',
      },
      constraints: false,
    });







//end loop



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

    db.exam_attempts.belongsTo(db.students, {
      as: 'student',
      foreignKey: {
        name: 'studentId',
      },
      constraints: false,
    });

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




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

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



  return exam_attempts;
};


