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

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  quiz_submissions.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity





















    db.quiz_submissions.hasMany(db.quiz_answers, {
      as: 'quiz_answers_quiz_submission',
      foreignKey: {
          name: 'quiz_submissionId',
      },
      constraints: false,
    });



























//end loop



    db.quiz_submissions.belongsTo(db.quizzes, {
      as: 'quiz',
      foreignKey: {
        name: 'quizId',
      },
      constraints: false,
    });

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

    db.quiz_submissions.belongsTo(db.mission_attempts, {
      as: 'mission_attempt',
      foreignKey: {
        name: 'mission_attemptId',
      },
      constraints: false,
    });

    db.quiz_submissions.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return quiz_submissions;
};


