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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"assigned",


"in_progress",


"completed",


"overdue",


"waived"

        ],

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

score: {
        type: DataTypes.DECIMAL,
      
      

      },

trainer_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  training_assignments.associate = (db) => {


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


































//end loop



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

    db.training_assignments.belongsTo(db.training_courses, {
      as: 'course',
      foreignKey: {
        name: 'courseId',
      },
      constraints: false,
    });

    db.training_assignments.belongsTo(db.documents, {
      as: 'document',
      foreignKey: {
        name: 'documentId',
      },
      constraints: false,
    });



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


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

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



  return training_assignments;
};


