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

name: {
        type: DataTypes.TEXT,
      
      

      },

cpf: {
        type: DataTypes.TEXT,
      
      

      },

birth_date: {
        type: DataTypes.DATE,
      
      

      },

relationship_code: {
        type: DataTypes.TEXT,
      
      

      },

student: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

health_plan: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

own_income: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  dependents.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity



















    db.dependents.hasMany(db.deductible_payments, {
      as: 'deductible_payments_dependent',
      foreignKey: {
          name: 'dependentId',
      },
      constraints: false,
    });













//end loop



    db.dependents.belongsTo(db.declarations, {
      as: 'declaration',
      foreignKey: {
        name: 'declarationId',
      },
      constraints: false,
    });

    db.dependents.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return dependents;
};


