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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

submission_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"offline",


"online"

        ],

      },

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

      },

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

  assignments.associate = (db) => {


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


















    db.assignments.hasMany(db.assignment_submissions, {
      as: 'assignment_submissions_assignment',
      foreignKey: {
          name: 'assignmentId',
      },
      constraints: false,
    });













//end loop



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

    db.assignments.belongsTo(db.users, {
      as: 'created_by',
      foreignKey: {
        name: 'created_byId',
      },
      constraints: false,
    });

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



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


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

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



  return assignments;
};


