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

name: {
        type: DataTypes.TEXT,
      
      

      },

assessment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"quiz",


"homework",


"project",


"midterm",


"final",


"other"

        ],

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

max_score: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"closed"

        ],

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  assessments.associate = (db) => {


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
























    db.assessments.hasMany(db.assessment_results, {
      as: 'assessment_results_assessment',
      foreignKey: {
          name: 'assessmentId',
      },
      constraints: false,
    });






//end loop



    db.assessments.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.assessments.belongsTo(db.class_subjects, {
      as: 'class_subject',
      foreignKey: {
        name: 'class_subjectId',
      },
      constraints: false,
    });



    db.assessments.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.assessments.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return assessments;
};


