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

name: {
        type: DataTypes.TEXT,
      
      

      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

website: {
        type: DataTypes.TEXT,
      
      

      },

industry: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

base_currency: {
        type: DataTypes.TEXT,
      
      

      },

onboarded_at: {
        type: DataTypes.DATE,
      
      

      },

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

  workspaces.associate = (db) => {


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







    db.workspaces.hasMany(db.workspace_memberships, {
      as: 'workspace_memberships_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.data_connections, {
      as: 'data_connections_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.financial_accounts, {
      as: 'financial_accounts_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.vendors, {
      as: 'vendors_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.customers, {
      as: 'customers_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.transactions, {
      as: 'transactions_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.budgets, {
      as: 'budgets_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });



    db.workspaces.hasMany(db.forecasts, {
      as: 'forecasts_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });



    db.workspaces.hasMany(db.metrics_snapshots, {
      as: 'metrics_snapshots_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.anomalies, {
      as: 'anomalies_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.alerts, {
      as: 'alerts_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.recommendations, {
      as: 'recommendations_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.chat_threads, {
      as: 'chat_threads_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });




    db.workspaces.hasMany(db.files_library, {
      as: 'files_library_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });


    db.workspaces.hasMany(db.sync_jobs, {
      as: 'sync_jobs_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });



//end loop



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

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




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

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



  return workspaces;
};


