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

      nome: {
        type: DataTypes.TEXT,
      },

      email: {
        type: DataTypes.TEXT,
      },

      senha_hash: {
        type: DataTypes.TEXT,
      },

      ativo: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      data_criacao: {
        type: DataTypes.DATE,
      },

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

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

    //end loop

    db.usuarios.belongsTo(db.roles, {
      as: 'role',
      foreignKey: {
        name: 'roleId',
      },
      constraints: false,
    });

    db.usuarios.belongsTo(db.clientes, {
      as: 'cliente',
      foreignKey: {
        name: 'clienteId',
      },
      constraints: false,
    });

    db.usuarios.belongsTo(db.clientes, {
      as: 'clientes',
      foreignKey: {
        name: 'clientesId',
      },
      constraints: false,
    });

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

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

  return usuarios;
};
