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,
      
      

      },

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

      },

onboarded_at: {
        type: DataTypes.DATE,
      
      

      },

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


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


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


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


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



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





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


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


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


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


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


