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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"needs_revision",


"accepted"

        ],

      },

submission_text: {
        type: DataTypes.TEXT,
      
      

      },

submission_url: {
        type: DataTypes.TEXT,
      
      

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

reviewed_at: {
        type: DataTypes.DATE,
      
      

      },

ai_feedback: {
        type: DataTypes.TEXT,
      
      

      },

ai_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  task_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.task_submissions.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.task_submissions.belongsTo(db.build_tasks, {
      as: 'build_task',
      foreignKey: {
        name: 'build_taskId',
      },
      constraints: false,
    });

    db.task_submissions.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return task_submissions;
};


