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

homework_title: {
        type: DataTypes.TEXT,
      
      

      },

homework_description: {
        type: DataTypes.TEXT,
      
      

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

late_until_at: {
        type: DataTypes.DATE,
      
      

      },

homework_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"closed",


"archived"

        ],

      },

max_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

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

  homeworks.associate = (db) => {


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












    db.homeworks.hasMany(db.homework_rubrics, {
      as: 'homework_rubrics_homework',
      foreignKey: {
          name: 'homeworkId',
      },
      constraints: false,
    });


    db.homeworks.hasMany(db.homework_extensions, {
      as: 'homework_extensions_homework',
      foreignKey: {
          name: 'homeworkId',
      },
      constraints: false,
    });


    db.homeworks.hasMany(db.homework_submissions, {
      as: 'homework_submissions_homework',
      foreignKey: {
          name: 'homeworkId',
      },
      constraints: false,
    });





    db.homeworks.hasMany(db.gradebook_columns, {
      as: 'gradebook_columns_homework',
      foreignKey: {
          name: 'homeworkId',
      },
      constraints: false,
    });





    db.homeworks.hasMany(db.notifications, {
      as: 'notifications_homework',
      foreignKey: {
          name: 'homeworkId',
      },
      constraints: false,
    });










//end loop



    db.homeworks.belongsTo(db.classrooms, {
      as: 'classroom',
      foreignKey: {
        name: 'classroomId',
      },
      constraints: false,
    });

    db.homeworks.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.homeworks.belongsTo(db.users, {
      as: 'teacher',
      foreignKey: {
        name: 'teacherId',
      },
      constraints: false,
    });

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



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


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

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



  return homeworks;
};


