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

      submission_date: {
        type: DataTypes.DATE,
      },

      content: {
        type: DataTypes.TEXT,
      },

      grade: {
        type: DataTypes.DECIMAL,
      },

      feedback: {
        type: DataTypes.TEXT,
      },

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

  assignment_submissions.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.assignment_submissions.belongsTo(db.assignments, {
      as: 'assignment',
      foreignKey: {
        name: 'assignmentId',
      },
      constraints: false,
    });

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

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

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

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

  return assignment_submissions;
};
