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

      modelo: {
        type: DataTypes.TEXT,
      },

      fabricante: {
        type: DataTypes.TEXT,
      },

      local_instalacao: {
        type: DataTypes.TEXT,
      },

      coordenada_lat: {
        type: DataTypes.DECIMAL,
      },

      coordenada_lon: {
        type: DataTypes.DECIMAL,
      },

      altura_instalacao_m: {
        type: DataTypes.DECIMAL,
      },

      angulo_focal: {
        type: DataTypes.TEXT,
      },

      ip_acesso: {
        type: DataTypes.TEXT,
      },

      status_operacional: {
        type: DataTypes.TEXT,
      },

      data_instalacao: {
        type: DataTypes.DATEONLY,

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

      observacoes: {
        type: DataTypes.TEXT,
      },

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

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

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

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

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

  return cameras_ptz;
};
