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

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

submission_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"resubmitted",


"late",


"missing",


"returned"

        ],

      },

submission_text: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

face_verified_at: {
        type: DataTypes.DATE,
      
      

      },

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

  homework_submissions.associate = (db) => {


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















    db.homework_submissions.hasMany(db.submission_grades, {
      as: 'submission_grades_submission',
      foreignKey: {
          name: 'submissionId',
      },
      constraints: false,
    });








    db.homework_submissions.hasMany(db.notifications, {
      as: 'notifications_submission',
      foreignKey: {
          name: 'submissionId',
      },
      constraints: false,
    });










//end loop



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

    db.homework_submissions.belongsTo(db.users, {
      as: 'student',
      foreignKey: {
        name: 'studentId',
      },
      constraints: false,
    });

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



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

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


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

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



  return homework_submissions;
};


