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

      tipo: {
        type: DataTypes.TEXT,
      },

      arquivo_url: {
        type: DataTypes.TEXT,
      },

      descricao: {
        type: DataTypes.TEXT,
      },

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

  gemeos_digitais.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.gemeos_digitais.belongsTo(db.inspecao_aerea, {
      as: 'inspecao',
      foreignKey: {
        name: 'inspecaoId',
      },
      constraints: false,
    });

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

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

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

  return gemeos_digitais;
};
