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

title: {
        type: DataTypes.TEXT,
      
      

      },

node_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"idea",


"problem",


"solution",


"customer_segment",


"value_proposition",


"channel",


"revenue_stream",


"cost_structure",


"key_activity",


"key_resource",


"key_partner",


"competitor",


"assumption",


"risk",


"metric",


"milestone",


"task",


"note"

        ],

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

x_position: {
        type: DataTypes.DECIMAL,
      
      

      },

y_position: {
        type: DataTypes.DECIMAL,
      
      

      },

confidence_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high"

        ],

      },

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

      },

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

  map_nodes.associate = (db) => {

    db.map_nodes.belongsToMany(db.node_evidence_items, {
      as: 'evidence_items',
      foreignKey: {
        name: 'map_nodes_evidence_itemsId',
      },
      constraints: false,
      through: 'map_nodesEvidence_itemsNode_evidence_items',
    });

    db.map_nodes.belongsToMany(db.node_evidence_items, {
      as: 'evidence_items_filter',
      foreignKey: {
        name: 'map_nodes_evidence_itemsId',
      },
      constraints: false,
      through: 'map_nodesEvidence_itemsNode_evidence_items',
    });


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













    db.map_nodes.hasMany(db.node_evidence_items, {
      as: 'node_evidence_items_node',
      foreignKey: {
          name: 'nodeId',
      },
      constraints: false,
    });









//end loop



    db.map_nodes.belongsTo(db.mind_maps, {
      as: 'mind_map',
      foreignKey: {
        name: 'mind_mapId',
      },
      constraints: false,
    });

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




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

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



  return map_nodes;
};


