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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"frozen",


"archived"

        ],

      },

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

  mind_maps.associate = (db) => {

    db.mind_maps.belongsToMany(db.map_nodes, {
      as: 'nodes',
      foreignKey: {
        name: 'mind_maps_nodesId',
      },
      constraints: false,
      through: 'mind_mapsNodesMap_nodes',
    });

    db.mind_maps.belongsToMany(db.map_nodes, {
      as: 'nodes_filter',
      foreignKey: {
        name: 'mind_maps_nodesId',
      },
      constraints: false,
      through: 'mind_mapsNodesMap_nodes',
    });

    db.mind_maps.belongsToMany(db.map_edges, {
      as: 'edges',
      foreignKey: {
        name: 'mind_maps_edgesId',
      },
      constraints: false,
      through: 'mind_mapsEdgesMap_edges',
    });

    db.mind_maps.belongsToMany(db.map_edges, {
      as: 'edges_filter',
      foreignKey: {
        name: 'mind_maps_edgesId',
      },
      constraints: false,
      through: 'mind_mapsEdgesMap_edges',
    });


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










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


    db.mind_maps.hasMany(db.map_edges, {
      as: 'map_edges_mind_map',
      foreignKey: {
          name: 'mind_mapId',
      },
      constraints: false,
    });











//end loop



    db.mind_maps.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

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




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

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



  return mind_maps;
};


