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

name: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"private",


"tenant"

        ],

      },

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

  dashboards.associate = (db) => {

    db.dashboards.belongsToMany(db.dashboard_widgets, {
      as: 'widgets',
      foreignKey: {
        name: 'dashboards_widgetsId',
      },
      constraints: false,
      through: 'dashboardsWidgetsDashboard_widgets',
    });

    db.dashboards.belongsToMany(db.dashboard_widgets, {
      as: 'widgets_filter',
      foreignKey: {
        name: 'dashboards_widgetsId',
      },
      constraints: false,
      through: 'dashboardsWidgetsDashboard_widgets',
    });


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






















































    db.dashboards.hasMany(db.dashboard_widgets, {
      as: 'dashboard_widgets_dashboard',
      foreignKey: {
          name: 'dashboardId',
      },
      constraints: false,
    });



//end loop



    db.dashboards.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.dashboards.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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




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

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



  return dashboards;
};


