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,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"suspended",


"trial"

        ],

      },

trial_ends_at: {
        type: DataTypes.DATE,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

default_currency: {
        type: DataTypes.TEXT,
      
      

      },

primary_domain: {
        type: DataTypes.TEXT,
      
      

      },

logo_url: {
        type: DataTypes.TEXT,
      
      

      },

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



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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


