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 agences = sequelize.define(
    'agences',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

      name: {
        type: DataTypes.TEXT,
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  agences.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.agences.hasMany(db.users, {
      as: 'users_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.claims, {
      as: 'claims_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.clients, {
      as: 'clients_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.company_profiles, {
      as: 'company_profiles_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.drivers, {
      as: 'drivers_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.expenses, {
      as: 'expenses_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.guarantees, {
      as: 'guarantees_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.infractions, {
      as: 'infractions_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.invoices_saas, {
      as: 'invoices_saas_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.maintenance, {
      as: 'maintenance_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.payments, {
      as: 'payments_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.plans, {
      as: 'plans_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.reservations, {
      as: 'reservations_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.subscriptions, {
      as: 'subscriptions_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

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

    db.agences.hasMany(db.vehicle_conditions, {
      as: 'vehicle_conditions_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    db.agences.hasMany(db.vehicles, {
      as: 'vehicles_agences',
      foreignKey: {
        name: 'agencesId',
      },
      constraints: false,
    });

    //end loop

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

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

  return agences;
};
