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,
      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

trade_name: {
        type: DataTypes.TEXT,
      
      

      },

tax_identifier: {
        type: DataTypes.TEXT,
      
      

      },

country_code: {
        type: DataTypes.TEXT,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

      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.tenant_memberships, {
      as: 'tenant_memberships_tenant',
      foreignKey: {
          name: 'tenantId',
      },
      constraints: false,
    });








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


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



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



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


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


















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






    db.tenants.hasMany(db.audit_events, {
      as: 'audit_events_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;
};


