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

name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"travel",


"calendar",


"meetings",


"messaging",


"email",


"payments",


"files",


"system",


"custom"

        ],

      },

execution_preference: {
        type: DataTypes.ENUM,
      
      

        values: [

"api_first",


"automation_first",


"native_first"

        ],

      },

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

      },

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

      },

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

      },

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

  tools.associate = (db) => {


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














    db.tools.hasMany(db.tool_actions, {
      as: 'tool_actions_tool',
      foreignKey: {
          name: 'toolId',
      },
      constraints: false,
    });






    db.tools.hasMany(db.tool_runs, {
      as: 'tool_runs_tool',
      foreignKey: {
          name: 'toolId',
      },
      constraints: false,
    });













//end loop



    db.tools.belongsTo(db.tool_providers, {
      as: 'provider',
      foreignKey: {
        name: 'providerId',
      },
      constraints: false,
    });

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




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

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



  return tools;
};


