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,
      
      

      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

primary_domain: {
        type: DataTypes.TEXT,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

default_currency: {
        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) => {

    db.tenants.belongsToMany(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'tenants_organizationsId',
      },
      constraints: false,
      through: 'tenantsOrganizationsOrganizations',
    });

    db.tenants.belongsToMany(db.organizations, {
      as: 'organizations_filter',
      foreignKey: {
        name: 'tenants_organizationsId',
      },
      constraints: false,
      through: 'tenantsOrganizationsOrganizations',
    });

    db.tenants.belongsToMany(db.properties, {
      as: 'properties',
      foreignKey: {
        name: 'tenants_propertiesId',
      },
      constraints: false,
      through: 'tenantsPropertiesProperties',
    });

    db.tenants.belongsToMany(db.properties, {
      as: 'properties_filter',
      foreignKey: {
        name: 'tenants_propertiesId',
      },
      constraints: false,
      through: 'tenantsPropertiesProperties',
    });

    db.tenants.belongsToMany(db.audit_logs, {
      as: 'audit_logs',
      foreignKey: {
        name: 'tenants_audit_logsId',
      },
      constraints: false,
      through: 'tenantsAudit_logsAudit_logs',
    });

    db.tenants.belongsToMany(db.audit_logs, {
      as: 'audit_logs_filter',
      foreignKey: {
        name: 'tenants_audit_logsId',
      },
      constraints: false,
      through: 'tenantsAudit_logsAudit_logs',
    });


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



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







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




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



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


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




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


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


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


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


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



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



//end loop






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

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



  return tenants;
};


