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,
      
      

      },

plan: {
        type: DataTypes.ENUM,
      
      

        values: [

"free",


"pro",


"team",


"enterprise"

        ],

      },

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

      },

trial_ends_at: {
        type: DataTypes.DATE,
      
      

      },

billing_email: {
        type: DataTypes.TEXT,
      
      

      },

      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.assistant_profiles, {
      as: 'assistant_profiles_workspace',
      foreignKey: {
          name: 'workspaceId',
      },
      constraints: false,
    });








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















    db.workspaces.hasMany(db.audit_logs, {
      as: 'audit_logs_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;
};


