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

external_task_key: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

task_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"created",


"failed",


"closed"

        ],

      },

created_at_remote: {
        type: DataTypes.DATE,
      
      

      },

failure_reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  tasks.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.tasks.belongsTo(db.knowledge_graph_items, {
      as: 'knowledge_graph_item',
      foreignKey: {
        name: 'knowledge_graph_itemId',
      },
      constraints: false,
    });

    db.tasks.belongsTo(db.external_integrations, {
      as: 'external_integration',
      foreignKey: {
        name: 'external_integrationId',
      },
      constraints: false,
    });

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




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

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



  return tasks;
};


