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

name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

plan_tier: {
        type: DataTypes.ENUM,
      
      

        values: [

"free",


"pro",


"business",


"enterprise"

        ],

      },

tenant_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"trial",


"past_due",


"suspended",


"closed"

        ],

      },

activated_at: {
        type: DataTypes.DATE,
      
      

      },

suspended_at: {
        type: DataTypes.DATE,
      
      

      },

default_region: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

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

      },

data_retention_policy: {
        type: DataTypes.TEXT,
      
      

      },

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

  tenants.associate = (db) => {


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










    db.tenants.hasMany(db.identity_providers, {
      as: 'identity_providers_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });



    db.tenants.hasMany(db.policy_rules, {
      as: 'policy_rules_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.kms_configs, {
      as: 'kms_configs_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.conference_templates, {
      as: 'conference_templates_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });











    db.tenants.hasMany(db.files, {
      as: 'files_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });







    db.tenants.hasMany(db.layout_models, {
      as: 'layout_models_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });






    db.tenants.hasMany(db.external_integrations, {
      as: 'external_integrations_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });



    db.tenants.hasMany(db.courses, {
      as: 'courses_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });







    db.tenants.hasMany(db.marketplace_apps, {
      as: 'marketplace_apps_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });




    db.tenants.hasMany(db.audit_events, {
      as: 'audit_events_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });


    db.tenants.hasMany(db.data_subject_requests, {
      as: 'data_subject_requests_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });




    db.tenants.hasMany(db.feature_flags, {
      as: 'feature_flags_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });



    db.tenants.hasMany(db.event_streams, {
      as: 'event_streams_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });




//end loop



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




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

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



  return tenants;
};


