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

      subdomain: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['active', 'suspended', 'trial'],
      },

      trial_ends_at: {
        type: DataTypes.DATE,
      },

      billing_email: {
        type: DataTypes.TEXT,
      },

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    //end loop

    db.tenants.belongsTo(db.plans, {
      as: 'plan',
      foreignKey: {
        name: 'planId',
      },
      constraints: false,
    });

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

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

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

  return tenants;
};
