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

attempt_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"not_started",


"in_progress",


"submitted",


"completed",


"failed",


"skipped"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

score: {
        type: DataTypes.DECIMAL,
      
      

      },

points_earned: {
        type: DataTypes.DECIMAL,
      
      

      },

coins_earned: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  mission_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.mission_attempts.hasMany(db.quiz_submissions, {
      as: 'quiz_submissions_mission_attempt',
      foreignKey: {
          name: 'mission_attemptId',
      },
      constraints: false,
    });




























//end loop



    db.mission_attempts.belongsTo(db.missions, {
      as: 'mission',
      foreignKey: {
        name: 'missionId',
      },
      constraints: false,
    });

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

    db.mission_attempts.belongsTo(db.enrollments, {
      as: 'enrollment',
      foreignKey: {
        name: 'enrollmentId',
      },
      constraints: false,
    });

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




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

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



  return mission_attempts;
};


