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

      mes_referencia: {
        type: DataTypes.DATEONLY,

        get: function () {
          return this.getDataValue('mes_referencia')
            ? moment
                .utc(this.getDataValue('mes_referencia'))
                .format('YYYY-MM-DD')
            : null;
        },
      },

      resumo: {
        type: DataTypes.TEXT,
      },

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

  relatorios_mensais.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.relatorios_mensais.belongsTo(db.obras, {
      as: 'obra',
      foreignKey: {
        name: 'obraId',
      },
      constraints: false,
    });

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

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

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

  return relatorios_mensais;
};
