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,
      },

title: {
        type: DataTypes.TEXT,
      
      

      },

goal: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"needs_info",


"planned",


"waiting_confirmation",


"running",


"completed",


"failed",


"canceled"

        ],

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"normal",


"high",


"urgent"

        ],

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

      },

      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


















    db.tasks.hasMany(db.task_steps, {
      as: 'task_steps_task',
      foreignKey: {
          name: 'taskId',
      },
      constraints: false,
    });



    db.tasks.hasMany(db.confirmations, {
      as: 'confirmations_task',
      foreignKey: {
          name: 'taskId',
      },
      constraints: false,
    });












//end loop



    db.tasks.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.tasks.belongsTo(db.conversations, {
      as: 'conversation',
      foreignKey: {
        name: 'conversationId',
      },
      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;
};


