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

      nome_planta: {
        type: DataTypes.TEXT,
      },

      descricao: {
        type: DataTypes.TEXT,
      },

      arquivo_digital: {
        type: DataTypes.TEXT,
      },

      formato: {
        type: DataTypes.TEXT,
      },

      data_criacao: {
        type: DataTypes.DATEONLY,

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

      responsavel_tecnico: {
        type: DataTypes.TEXT,
      },

      georreferenciada: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      sistema_referencia: {
        type: DataTypes.TEXT,
      },

      comentarios: {
        type: DataTypes.TEXT,
      },

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

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

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

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

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

  return plantas_asbuilt_2d;
};
